Home > OS >  How to correctly print the created label?
How to correctly print the created label?

Time:01-18

I entered a number and generated a barcode. However, I can't print out the size I want from the printer. How can I adjust the dimensions of the label? I am using Godex G300 model printer.

Here is the code I tried :

private void GenerateBarcodeButton_Click(object sender, EventArgs e)
{
    if (!String.IsNullOrEmpty(textBox1.Text))
    {
        Zen.Barcode.Code128BarcodeDraw barcodeDraw = Zen.Barcode.BarcodeDrawFactory.Code128WithChecksum;
        pictureBox1.Image = barcodeDraw.Draw(textBox1.Text, 50);
    }
    else
        MessageBox.Show("Please enter the barcode number you want to generate.");
}
public void PrintPicture(object sender, PrintPageEventArgs e)
{
    Bitmap bmp = new Bitmap(pictureBox1.Width, pictureBox1.Height);
    pictureBox1.DrawToBitmap(bmp, new Rectangle(0, 0, pictureBox1.Width, pictureBox1.Height));
    e.Graphics.DrawImage(bmp, 0, 0);
    bmp.Dispose();
}

private void Print_Click(object sender, EventArgs e)
{
    PrintDialog pd = new PrintDialog();
    PrintDocument pDoc = new PrintDocument();
    pDoc.PrintPage  = PrintPicture;
    pd.Document = pDoc;
    if (pd.ShowDialog() == DialogResult.OK)
    {
        pDoc.Print();
    }
}

CodePudding user response:

So a few things to consider (no short answer I'm sorry):

First (optionally) setting the media size of the paper or label in the printer (this can also be set in the Printer Defaults):

 pDoc.DefaultPageSettings.PaperSize = new System.Drawing.Printing.PaperSize("Temp", System.Convert.ToInt32((pageSize.Height / (double)96) * 100), System.Convert.ToInt32((pageSize.Width / (double)96) * 100)); //Convert from 96dpi to 100dpi 

Then when rendering your image you need to specify the width and height you want to scale and draw at:

e.Graphics.DrawImage(bmp, 0, 0, New System.Drawing.RectangleF(0, 0, width, height), System.Drawing.GraphicsUnit.Pixel)

HOWEVER I recommend keeping your images original width to avoid blurring which can affect the quality of your barcode and potentially result in it being rejected if used in a commercial environment.

Instead generate a bigger image to begin with.

e.Graphics.DrawImage(bmp, 0, 0, New System.Drawing.RectangleF(0, 0, bmp.Width, bmp.Height), System.Drawing.GraphicsUnit.Pixel)

Lastly you need to consider the DPI of your printer which is 203 DPI

If you try and print a 96dpi image at 203dpi some pixels in your image will span a non-integer number of dots on the printer. When this happens label printers use a process called dithering which will either round partial pixels up/down or alternate rows of pixels to average out. This will result in the lines of your barcode being narrower/wider than they should be or lines with jagged edges.

To avoid this ensure your image DPI matches the DPI of your printer (or your printer DPI can be divided by your image DPI exactly)

Other solutions:

  • Communicate with the printer directly using a command language it supports (EZPL: https://www.godexprinters.co.uk/desktop/g300)
  • Buy label printing software such as CodeSoft or BarTender which you design your labels in and interface with in .NET by passing a label file path and set of variable data.

The following code works for me (Honeywell PM43c 203dpi) (note the use of GraphicsUnit.Pixel)

public Form1()
        {
            InitializeComponent();
            pictureBox1.SizeMode = PictureBoxSizeMode.Normal;
            pictureBox1.BackColor = Color.White;
        }

        private void GenerateBarcodeButton_Click(object sender, EventArgs e)
        {
            if (!String.IsNullOrEmpty(textBox1.Text))
            {
                pictureBox1.Image = Zen.Barcode.BarcodeDrawFactory.Code128WithChecksum.Draw(textBox1.Text, 50, 2);
            }
            else
            {
                MessageBox.Show("Please enter the barcode number you want to generate.");
            }
        }

        public void PrintPicture(object sender, PrintPageEventArgs e)
        {
            Bitmap bmp = new Bitmap(pictureBox1.Width, pictureBox1.Height);
            pictureBox1.DrawToBitmap(bmp, new Rectangle(0, 0, pictureBox1.Width, pictureBox1.Height));
            e.Graphics.DrawImage(bmp, 20, 20, new System.Drawing.RectangleF(0, 0, bmp.Width, bmp.Height), System.Drawing.GraphicsUnit.Pixel);
        }

        private void PrintButton_Click(object sender, EventArgs e)
        {
            PrintDialog pd = new PrintDialog();
            PrintDocument pDoc = new PrintDocument();
            pDoc.PrintPage  = PrintPicture;
            pd.Document = pDoc;
            if (pd.ShowDialog() == DialogResult.OK)
            {
                pDoc.Print();
            }
        }
  • Related