0
votes

I 'm using the code below to print the barcode that gets generated in my C# WinForms, but the barcode scanner does not detect it, I've tried using code 128 font and code39 fonts with no luck, when I print using the bartender software it detects the barcode generated, just won't detect mine

here is the code

 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;


            }  
        }

I have tried adding and removing the * as suggested in other posts, also changing the font size from 8p went one by one till 28px, but still no luck the scanner and the printer are working fine on the bartender application

The code used in the example below is 5094411

Here is an image, the textbox highlighted in red is where the string barcode is coming from

and you can see in the image that in the picturebox it shows 5094411 in both IDautomationHC39M font (the barcode font) and again below it in Arial

enter image description here

1
IDAutomationHC39M is a Code39 font and cannot output Code128 barcodes. Note also that barcodes have checksums which must be precalculated if you are using barcode fonts. Please include example barcode strings. - Dour High Arch
@DourHighArch I'm not really sure what you mean by precalculated, the strings I'm using to generate are just numbers like 12345678 I have a random number generator to a textbox, and using this value to the barcode string so it is just a number - Joey Arnanzo
I think it's the 4th-5th time that I see the (almost) exact same code with the exact same mistakes in a couple of months. Is this a sample code coming from the Barcode font producer? Anyway, what are the dimensions of the Bitmap you have to print, ie. do you have to print it in a defined space, or it can be large ad libitum? Or do you just need to create a Bitmap and then stretch it to fit? What do you mean with "does not detect"? The barcode scanner does not read it or it outputs wrong numbers? - Jimi
@Jimi I'm trying to use a Xprinter label barcode printer, the paper size is 2" x 1" .. what I mean that it does not read it at all, no beep, nothing, it only detects if I print using the Bartender software that came with the printer - Joey Arnanzo
Take a look at this: Blurry and large image barcode. Btw, Code39 does not need a check digit, you can input raw data and the * start-stop symbol is needed. - Jimi

1 Answers

0
votes

This is the correct code that after some tries with the help of Jimi here I was able to get this part right

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