Take the 2-minute tour ×

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...

share|improve this question
add comment

3 Answers

up vote 10 down vote accepted

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:

share|improve this answer
I completely agree with you, it's just that switching to one of these may not be as easy as switching a DLL... –  Brad Aug 11 '11 at 14:44
you can even use such a library to implement the MakeBarcodeImage method - this way there is no interface change and none of the other parts of your project need to be changed... –  Yahia Aug 11 '11 at 14:47
After doing some searching, I can easily see that this code is based off this: codeproject.com/KB/GDI-plus/GenCode128.aspx but has some changes... –  Brad Aug 11 '11 at 14:56
in the link you provided it explicitly states that they don't always conform with the spec since they don't enforce the minimum width, sounds like your issue exactly... –  Yahia Aug 11 '11 at 15:10
You are correct... I'm going to try to migrate to a new library... Thanks! –  Brad Aug 11 '11 at 15:13
add comment

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.


share|improve this answer
I have to agree, this is a poor routine. Barcodes are binary, it should use a constant line width with the binary codes from: en.wikipedia.org/wiki/Code_128 –  Martin Aug 11 '11 at 14:54
Thanks for the advice... I should probably look at a free library and take the time to get it working... –  Brad Aug 11 '11 at 15:01
add comment

If you don't want to use a free barcode library, as suggested by Yahia, I think that increasing the value of:settings.InterCharacterGap should do the trick.

However, I think you should take a look at those libraries. This way, you will have less code to maintain, will be able to adapt your software more easily and if the type of bar codes required changes, you might be able to support it right away instead of re-inventing the wheel.

share|improve this answer
add comment

Your Answer


