打印后未通过条形码扫描器检测到条形码

时间:2018-05-17 00:36:05

标签: c# barcode barcode-scanner

我使用下面的代码打印在我的C#WinForms中生成的条形码,但条形码扫描器没有检测到它,我尝试使用代码128字体和code39字体没有运气,当我使用调酒软件打印时,它检测到生成的条形码,只是没有检测到我的

这是代码

 private void txtPprice_TextChanged(object sender, EventArgs e)
        {
            string barcode = txtCode.Text;
            string price = txtPprice.Text;
            string pname = txtPname.Text;
            Bitmap bitm = new Bitmap(barcode.Length * 30, 90);

            using (Graphics graphic = Graphics.FromImage(bitm))
            {


                Font newfont = new Font("IDAutomationHC39M", 10);
                Font newfont2 = new Font("Arial Black", 8);
                PointF point = new PointF(10f, 10f);
                SolidBrush black = new SolidBrush(Color.Black);
                SolidBrush white = new SolidBrush(Color.White);
                graphic.FillRectangle(white, 0, 0, bitm.Width, bitm.Height);
                graphic.DrawString("*" + barcode + "*", newfont, black, point);
                PointF pointPrice = new PointF(45f, 55f);
                graphic.DrawString("" + pname +"", newfont2, black, pointPrice);
                PointF pointPname = new PointF(90f, 75f);
                graphic.DrawString("" + price + " L.E.", newfont2, black, pointPname);
                PointF pointBcode = new PointF(20f, 75f);
                graphic.DrawString("" + barcode + "", newfont2, black, pointBcode);

            }

            using (MemoryStream Mmst = new MemoryStream())
            {


                bitm.Save("ms", ImageFormat.Jpeg);
                pictureBox1.Image = bitm;
                pictureBox1.Width = bitm.Width;
                pictureBox1.Height = bitm.Height;


            }  
        }

我试过按照其他帖子的建议添加和删除*,同样将字体大小从8p逐个改为28px,但仍然没有运气 扫描仪和打印机在调酒师应用程序上正常工作

以下示例中使用的代码是5094411

这是一张图片,红色突出显示的文本框是字符串条形码的来源

你可以在图片中看到它在图片框中以IDautomationHC39M字体(条形码字体)显示5094411,在Arial中再次显示在它下面

enter image description here

1 个答案:

答案 0 :(得分:0)

这是正确的代码,在Jimi的帮助下经过一些尝试后,我能够使这一部分正确

string Barcode = "*"+txtCode.Text+"*";
            string price = txtPprice.Text;
            string pname = txtPname.Text;

            using (Bitmap bitmap = new Bitmap(350, 220))
            {
                bitmap.SetResolution(240, 240);
                using (Graphics graphics = Graphics.FromImage(bitmap))
                {
                    Font font = new Font("IDAutomationHC39M", 10, FontStyle.Regular, GraphicsUnit.Point);

                    graphics.Clear(Color.White);
                    StringFormat stringformat = new StringFormat(StringFormatFlags.NoWrap);
                    graphics.TextRenderingHint = TextRenderingHint.AntiAliasGridFit;
                    graphics.TextContrast = 10;
                    SolidBrush black = new SolidBrush(Color.Black);
                    SolidBrush white = new SolidBrush(Color.White);
                    PointF TextPosition = new PointF(45F, 10F);
                    SizeF TextSize = graphics.MeasureString(Barcode, font, TextPosition, stringformat);
                    PointF pointPrice = new PointF(90f, 125f);
                    Font newfont2 = new Font("Cambria", 8, FontStyle.Regular, GraphicsUnit.Point);
                    Font newfont3 = new Font("Arial Black", 10, FontStyle.Regular, GraphicsUnit.Point);
                    graphics.DrawString("" + pname + "", newfont3, black, pointPrice);
                    PointF pointPname = new PointF(200f, 170f);
                    graphics.DrawString("" + price + " L.E.", newfont3, black, pointPname);
                    PointF pointBcode = new PointF(35f, 170f);
                    graphics.DrawString("" + Barcode + "", newfont2, black, pointBcode);
                    if (TextSize.Width > bitmap.Width)
                    {
                        float ScaleFactor = (bitmap.Width - (TextPosition.X / 2)) / TextSize.Width;
                        graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
                        graphics.ScaleTransform(ScaleFactor, ScaleFactor);
                    }

                    graphics.DrawString(Barcode, font, new SolidBrush(Color.Black), TextPosition, StringFormat.GenericTypographic);

                    bitmap.Save(@"barcode.png", ImageFormat.Png);
                    this.pictureBox1.Image = (Bitmap)bitmap.Clone();
                    font.Dispose();
                }
            }