Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generate barcode with Free 3 of 9 font

Tags:

c#

barcode

public Bitmap CreateBarcode(string data)
{
    data = "55536"; 
    string barcodeData = "*" + data + "*";
    Bitmap barcode = new Bitmap(1, 1);
    Font threeOfNine = new Font("Free 3 of 9 Extended", 31, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point);

    Font arial = new Font("Arial", 13,
              System.Drawing.FontStyle.Regular,
              System.Drawing.GraphicsUnit.Point);

    Graphics graphics = Graphics.FromImage(barcode);
    SizeF dataSize = graphics.MeasureString(barcodeData, threeOfNine);
    dataSize.Height = 70;

    barcode = new Bitmap(barcode, dataSize.ToSize());
    graphics = Graphics.FromImage(barcode);

    graphics.Clear(Color.White);
    graphics.TextRenderingHint = TextRenderingHint.SingleBitPerPixel;

    graphics.DrawString(barcodeData, threeOfNine, new SolidBrush(Color.Black), 0, 0);

    graphics.DrawString(data, arial, new SolidBrush(Color.Black), 50, 40);

    graphics.Flush();

    threeOfNine.Dispose();
    graphics.Dispose();

    return barcode;
}

I generate barcode with the above code, but my scanner can not read the barcode generated (for 55536). BUT if I switch the data value to "1111" or "2222", then the barcode be read very well. so I think it is not a scanner problem, anybody know, what's the wrong with that code? please advice.

like image 890
Expert wanna be Avatar asked Nov 13 '22 06:11

Expert wanna be


1 Answers

If you are using only numbers, you could try the 3 of 9 basic font (without the extended). Print the same barcode from Write and compare them to see if your solution is building the complete barcode or if it is getting truncated.

like image 146
Doug L. Avatar answered Nov 15 '22 07:11

Doug L.