程序打印在不同OS版本上调整大小的图像

时间:2017-05-10 09:38:13

标签: c# winforms printing windows-7

我在目标机器和我自己的机器上都有一个工作代码。但是,我的机器是W10,目标是W7。下面的代码应该在页面的右半部分绘制5个条形码,但是,当我在目标机器上使用它时,会发生两种情况中的一种,具体取决于我如何更改代码。如果我使条形码的宽度太大,它将超出页面的边缘并且它将不可读。如果我将宽度略微过小,它会调整整个条形码的大小,然后变得很小,再次无法读取。我认为这是打印驱动程序的一个问题,它们对于W10和W7都是不同的(我在两台机器上使用相同的打印机,同样的设置)。 我的代码是否有问题,如何更改?

    private void DocumentDrucker_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
    {
        Graphics graphic = e.Graphics;
        //SolidBrush brush = new SolidBrush(Color.Black);

        Font font = new Font("Courier New", 80, FontStyle.Bold);
        Font fontK = new Font("Courier New", 30, FontStyle.Bold);
        Font fontKleinst = new Font("Courier New", 15, FontStyle.Bold);

        float pageWidth = e.PageSettings.PrintableArea.Width;
        float pageHeight = e.PageSettings.PrintableArea.Height;


        float fontHeight = font.GetHeight();
        int startX = 0;
        int startY = 0;
        int offsetY = 0;

        float imageH = Properties.Resources.pfeilO.Height;
        float imageW = Properties.Resources.pfeilO.Width;

        graphic.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.NearestNeighbor;

        //var imagePoint = new Point(Convert.ToInt32(pageWidth * 0.55), offsetY);
        var barcodePoint = new Point(Convert.ToInt32(pageWidth * 0.66), 0);

        for (; elemente < ZumDrucken.Items.Count; elemente++)
        {
            var currentItem = ZumDrucken.Items[elemente];
            graphic.DrawString(currentItem.Text.Substring(4, 3), fontK, Brushes.Black, startX, startY +offsetY+20);
            graphic.DrawString("Typ:"+currentItem.Text.Substring(0,3), fontKleinst, Brushes.Black, startX, (startY + offsetY + 30+fontK.Height));
            graphic.DrawString(currentItem.Text.Substring(7), font, Brushes.Black, (startX+75), startY + offsetY);

            var currentImage = Properties.Resources.pfeilU;

            bool lastCharIsAOne = currentItem.Text[currentItem.Text.Length - 1] == '1';
            if (currentItem.Checked == lastCharIsAOne)
            {
                currentImage = Properties.Resources.pfeilO;
            }
            graphic.DrawImage(currentImage, Convert.ToInt32((pageWidth * 0.55)), offsetY+20, imageW, imageH);

            b.EncodedImage?.Dispose();
            //b.Encode(TYPE.CODE128A, "SBD" + currentItem.Text.Substring(0, 3) + currentItem.Text.Substring(4), Color.Black, Color.Transparent,Convert.ToInt32(pageWidth-50), Convert.ToInt32(pageHeight * 0.151));
            b.Encode(TYPE.CODE128A, "SBD" + currentItem.Text.Substring(0, 3) + currentItem.Text.Substring(4), Color.Black, Color.Transparent, 570, 135);


            barcodePoint.Y = offsetY;
            graphic.DrawImage(b.EncodedImage, barcodePoint);

            offsetY = offsetY + 163;
            if (offsetY >= pageWidth-120)
            {
                e.HasMorePages = true;
                offsetY = 0;
                elemente++;

                graphic.Dispose();
                b.Dispose();
                font.Dispose();
                fontK.Dispose();
                fontKleinst.Dispose();

                return;
            }
            else
            {
                e.HasMorePages = false;
            }
        }
        graphic.Dispose();
        b.Dispose();
        font.Dispose();
        fontK.Dispose();
        fontKleinst.Dispose();

    }

这里也是我将所有边距设置为0的代码,并将纸张模式更改为横向。

    private void Drucken_Click(object sender, EventArgs e)
    {
        DocumentDrucker.DocumentName = "Dokument";
        elemente = 0;
        DruckDialog.Document = DocumentDrucker;



        DocumentDrucker.DefaultPageSettings.Landscape = true;
        DocumentDrucker.DefaultPageSettings.Margins.Top = 0;
        DocumentDrucker.DefaultPageSettings.Margins.Left = 0;
        DocumentDrucker.DefaultPageSettings.Margins.Right = 0;
        DocumentDrucker.DefaultPageSettings.Margins.Bottom = 0;




        if (DruckDialog.ShowDialog() == DialogResult.OK)
            DocumentDrucker.Print();
    }

0 个答案:

没有答案