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

3

9

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

Flunkey answered 11/8, 2011 at 14:27 Comment(1)
You can find GenCode128.dll in NuGet: nuget.org/packages/GenCode128 Also the source can be found in GitHub: github.com/SourceCodeBackup/GenCode128Bipropellant
P
15

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

Pluri answered 11/8, 2011 at 14:37 Comment(5)
I completely agree with you, it's just that switching to one of these may not be as easy as switching a DLL...Flunkey
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...Pluri
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...Flunkey
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...Pluri
You are correct... I'm going to try to migrate to a new library... Thanks!Flunkey
O
7

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

Order answered 11/8, 2011 at 14:50 Comment(2)
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_128Cringe
Thanks for the advice... I should probably look at a free library and take the time to get it working...Flunkey
C
2

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.

Cringe answered 11/8, 2011 at 14:51 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.