Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# : Generating Code 128 Barcode (width of bars/spaces)

So I inherited this code, or should I say, someone developed this and moved on and now we are having a problem with it and I'm looking into it...

We are generating c128 barcodes and upon having them certified they noticed an issue that I can't see to figure out. The width of a bars/spaces is 10.5 mils and they acceptable range is 15-21 mils (1 mil = .001 inch).

The rendering code is based off this library: http://www.codeproject.com/KB/GDI-plus/GenCode128.aspx but has been modified some...

The barcodes being generated are all alpha-numeric, no special characters. I thought the width of the bar + space was dependent on the character being encoded.

Here are the settings being used:

settings.Font = new Font ( FontFamily.GenericSansSerif, 12 );
settings.TopMargin = 10
settings.BottomMargin = 10
settings.LeftMargin = 10
settings.RightMargin = 10
settings.BarCodeHeight = 80
settings.DrawText = true
settings.BarCodeToTextGapHeight = 10
settings.InterCharacterGap = 2

If I was guessing, I think it's because the width of the bars is being based on the height of the barcode instead of the height of the barcode being based on the length of the text and barcode. But I'm not too familiar with the spec (even after reviewing it), and I'm a novice C# programmer at best...

like image 555
Brad Avatar asked Aug 11 '11 14:08

Brad


2 Answers

This isn't a direct answer BUT I would strongly recommend to use a well-tested library for generating barcodes... getting barcodes right isn't easy since there are some pitfalls... there are tons of libraries out there some commercial, some free...

2 free barcode libraries for .NET:
http://barcoderender.codeplex.com/
http://www.codeproject.com/KB/graphics/BarcodeLibrary.aspx

like image 105
Yahia Avatar answered Oct 10 '22 03:10

Yahia


FWIW, that is an extremely poor barcode generation routine and you should abandoned it and search for a library that has been specifically written to generate these barcodes.

What is the resolution of the device context used to make the bitmap?

From the looks of it, your code is using a screen device context by default, which is 96dpi.

Barcodes need to be generated at a minimum of 300dpi, preferably 600dpi and ideally 2540dpi.

At 96dpi you'll never achieve the resolution needed for the accuracy given.

Solution 1: Modify the code to use a high resolution printer device context and make the bitmap at that resolution. Currently your code is just using an arbitrarily computed width.

The next problem is that the code uses an integer bar width and casts to a float (yikes!). This becomes an issue when dealing with low dpi (even high dpi but not as much so) as some bars/spaces might be taking 2 pixels and some might be taking 3, so you end up with a barcode that has uneven bars/spaces.

Solution 2: Ensure all bars/spaces that are suppose to be the same width are the same width.

HTH

like image 24
TheBarcodeMan Avatar answered Oct 10 '22 03:10

TheBarcodeMan