Printing generated barcode

时间:2019-04-17 01:20:26

标签: c# winforms printing zen

Briefly, I'm trying printing generated code using Zen library and "Xprinter XP-235B", all seems and works well except the printer as it prints the Generated barcode wrong:-

  1. First, it skips 2 labels and print in the third one.

  2. sometimes print it incomplete, plus, The alignment is not in the center.

and this is the code I'm using

 private void txt_BarcodeGenerator_Click(object sender, EventArgs e)
        {
            if (txt_BarcodeGenerator.Text == "")
            {
                MessageBox.Show("قم بصنع باركود جديد بالضغط انتر ف حقل الباركود", "خطأ", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
            else
            {
                Zen.Barcode.Code128BarcodeDraw barcode = Zen.Barcode.BarcodeDrawFactory.Code128WithChecksum;
                var barcodeImage = barcode.Draw(txt_BarcodeGenerator.Text, 50);

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

                using (var graphics = Graphics.FromImage(resultImage))
                using (var font = new Font("Consolas", 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.Clear(Color.White);
                    graphics.DrawImage(barcodeImage, 0, 0);
                    graphics.DrawString(txt_BarcodeGenerator.Text, font, brush, resultImage.Width / 2, resultImage.Height, format);
                }

                pictureBox1.Image = resultImage;
                txt_BarcodeGenerator.Clear();
                txt_BarcodeGenerator.Focus();
            }
        }

   private void btn_PrintBarcode_Click(object sender, EventArgs e)
        {
            ((Form)printPreviewDialog1).WindowState = FormWindowState.Maximized;
            PrintDialog myPrinDialog1 = new PrintDialog();
            if (myPrinDialog1.ShowDialog() == DialogResult.OK)
            {
                printDocument1.Print();             
            }
        }

        private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
        {
            //e.Graphics.DrawImage(, 0, 0);
            //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);
            e.Graphics.DrawImage(pictureBox1.Image, 0, 0);
        }

0 个答案:

没有答案