0

I have a working code, on both the target machine, and my own machine. However, my machine is a W10, and the target is a W7. The code below should draw 5 barcodes on the right half of the page, however, when I use it on the target machine, one of two scenarios happen, depending on how I change the code. If I make the width of the barcode too big, it will go beyond the edge of the page and it will not be readable. If I make the width slightly too small, it resizes the whole barcode, and it becomes to small, again unreadable. I assume that this is a problem with the print drivers in which, they are different for both W10 and W7 (I used the same printer on both machines, same settings as well). Is there a problem with my code and how do I change it?

    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();

    }

Here is also the code where I set all the margins to 0, and change the paper mode to landscape.

    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();
    }
EInherjar
  • 339
  • 6
  • 17
  • if you need the same proportions on all target media, you should get rid of magic numbers like 163, 120, ... and express coordinates and font sizes as a fraction of the total logical page width/page height. Then everything will scale properly regardless of resolution (dpi) and physical media dimensions. – Cee McSharpface May 10 '17 at 10:09
  • Well, I did, the commented part in which the barcode is encoded is done like that, however, it does not resize correctly. If I leave it like it is, the barcode resizes and it becomes smaller. If I decrease the part to substract (-50 to -20), it cuts off the barcode. – EInherjar May 10 '17 at 10:37
  • [consider this](http://stackoverflow.com/a/8841688/1132334) for the behavior of the `PrintableArea` property. I think you have a non-zero origin of the printable area. It may also be worth to *print* the margin and printablearea rectangles so you can see where they end up on the paper in relation to your other output. – Cee McSharpface May 10 '17 at 10:53
  • I will try the code in the linked answer to test out the margins on the other printer. Meanwhile, I already set the margins to 0, as you can see in the edited question, so that should not be the problem. Also, if I run the program from my computer, the output is the same as if I would print it on a different printer. Is it really the drivers then? – EInherjar May 10 '17 at 11:16

0 Answers0