#StackBounty: #c# #winforms #barcode #picturebox #barcode-printing Why my bar code image does not fit the specified paper size in bar c…

Bounty: 50

I am using Zen Barcode Rendering Framework to create bar codes in C# windows form application. I have two text boxes (one for bar code itself and one for the relevant text that I want it to be printed on the bar code label). Similarly, I am loading the generated bar code image to a picture box and try to print that but every time I press the print button, the result is inappropriate (Sometimes the printer prints a white empty label and sometimes the bar code gets printed incomplete. Interestingly, I have to say that in order to make the bar code appear on the label even if it appears incomplete, I have to choose very large paper sizes). Here’s my code:

The code for my generate bar code button’s click event:

private void Button1_Click(object sender, EventArgs e)
        string barcode = textBox1.Text;

        Zen.Barcode.Code128BarcodeDraw brcd = Zen.Barcode.BarcodeDrawFactory.Code128WithChecksum;
        var barcodeImage = brcd.Draw(barcode, 50);

        int resultImageWidth;
        if(barcodeImage.Width >= textBox2.Text.Length*8)
            resultImageWidth = barcodeImage.Width;
            resultImageWidth = textBox2.Text.Length*8;

        var resultImage = new Bitmap(resultImageWidth, barcodeImage.Height + 60); // 20 is bottom padding, adjust to your text

        using (var graphics = Graphics.FromImage(resultImage))
        using (var font = new Font("IranYekan", 10))
        using (var brush = new SolidBrush(Color.Black))
        using (var format = new StringFormat()
            Alignment = StringAlignment.Center, // Also, horizontally centered text, as in your example of the expected output
            LineAlignment = StringAlignment.Far
            graphics.DrawImage(barcodeImage, (resultImageWidth - barcodeImage.Width)/2, 0);
            graphics.DrawString(textBox1.Text, font, brush, resultImage.Width / 2, resultImage.Height-30, format);
            graphics.DrawString(textBox2.Text, font, brush, resultImage.Width / 2, resultImage.Height, format);

        pictureBox1.Image = resultImage;


The code for my print button’s click event:

private void Button2_Click(object sender, EventArgs e)
    PrintDialog pd = new PrintDialog();
    PrintDocument doc = new PrintDocument();
    doc.PrintPage += Doc_PrintPage;
    pd.Document = doc;
    if (pd.ShowDialog() == DialogResult.OK)

And my Doc_PrintPage() function:

private void Doc_PrintPage(object sender, PrintPageEventArgs e)
    Bitmap bm = new Bitmap(pictureBox1.Width, pictureBox1.Height);
    pictureBox1.DrawToBitmap(bm, new Rectangle(0, 0, pictureBox1.Width, pictureBox1.Height));
    e.Graphics.DrawImage(bm, 0, 0);

My main goal is to print the bar code completely with its relevant text inside the paper bounds that gets selected when print dialog appears.

You can view my application’s UI in the image below:
enter image description here

Here are my printed results as you see they lack quality and the image does not fit correctly every time. I use Brother QL-700
enter image description here
enter image description here
enter image description here
enter image description here

Get this bounty!!!

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.