Barcode Image Generation Library
Barcode Image Generation Library
Barcode Image Generation Library
This library was designed to give an easy class for developers to use when they need to generate
barcode images from a string of data.
Download binaries
Download source
Introduction
http://www.codeproject.com/Articles/20823/Barcode-Image-Generation-Library?display=Print 1/10
9/30/2014 Barcode Image Generation Library - CodeProject
This article and its code provides a way for developers to put barcodes into their applications. It
allows for the generation of barcode images without the use of "barcode fonts". This need arose
out of the necessity for one of my own projects to use barcodes and the lack of free libraries on
the web to do the job.
To give an idea of what happens in this library: it first turns the data into a series of 1s and 0s
representing equal-width bars and spaces. This string of binary information is then sent to a
drawing function that converts it to an image representing the desired barcode. This approach
allows for one common drawing method to be used on all symbologies.
** Keep in mind that some symbologies go by more than one name, so make sure the one you
are using isn't listed above by a different name before contacting me to add it. If it isn't listed
above and you would like me to look at adding it to this library, please post a comment below,
and I will take a look at it as soon as possible. (Bugs are always a priority, so please send me bug
reports.)
Barcode();
Barcode(string);
Barcode (string, BarcodeLib.TYPE);
If you decide to create an instance with parameters, the parameters are as follows: the string is
the data to be encoded into the barcode, and BarcodeLib.TYPE is the symbology to encode
the data with. If you do not choose to specify the data and type at the time the instance is
created, you may specify them through the appropriate property later on (but before you
encode).
http://www.codeproject.com/Articles/20823/Barcode-Image-Generation-Library?display=Print 2/10
9/30/2014 Barcode Image Generation Library - CodeProject
To get the image representing the data generated, you must then call one of the many Encode
functions.
public Image Encode(TYPE iType, string StringToEncode, int Width, int Height)
public Image Encode(TYPE iType, string StringToEncode,
Color ForeColor, Color BackColor, int Width, int Height)
public Image Encode(TYPE iType, string StringToEncode, Color ForeColor, Color BackColor)
public Image Encode(TYPE iType, string StringToEncode)
Encode(TYPE iType)
Encode()
The resulting Image contains the barcode in image format. More functionality has been added,
so you can save the Image once it is encoded.
This function can be used by specifying the full path (filename included) of the location you
would like to save the image to as a string. The second parameter is an enumerator
(BarcodeLib.SaveTypes) that represents the supported types (JPG, BMP, PNG, GIF, TIFF) of
files you can save. Functionality has been added so that you can now set the IncludeLabel
parameter to allow it to draw the data that's encoded in the barcode, below the image, as a label.
Keep in mind that this will take up some of the space you specify for the size of the image.
b.IncludeLabel = true;
This is used to put the data encoded at the bottom of the image. If you do not set this
parameter, it will just generate the barcode without the data at the bottom.
Points of Interest
Writing this library offered me the chance to become intimately familiar with how barcode
symbologies work and how the symbologies differ from one another.
A new property has been added to allow exportation of the image and properties in XML format.
Keep in mind that the barcode must be encoded first before this property is read; otherwise, an
error will be thrown to notify you of this mistake.
b.XML
History
October 10, 2007
Initial release (bugs most certainly exist, and a couple of symbologies need to be
modified/implemented).
http://www.codeproject.com/Articles/20823/Barcode-Image-Generation-Library?display=Print 3/10
9/30/2014 Barcode Image Generation Library - CodeProject
November 1, 2007
December 9, 2007
int pos = 0;
http://www.codeproject.com/Articles/20823/Barcode-Image-Generation-Library?display=Print 4/10
9/30/2014 Barcode Image Generation Library - CodeProject
Code 128 support (beta) (the FNC* chars are still not encoding right... I have to
figure that out), but ... it's at least here for your trial and for your comments.
Also, there were a few country codes left out of the EAN-13 assigning country
lookup. I added what I could find that I left out.
Changed the CheckNumericOnly() back to a more complex task because some
data being encoded was longer than what Int64.TryParse(string, out) could
handle ... so back to a more complex, but still a faster, task than comparing each
char. Now, I break the string into pieces and test each piece.
May 3, 2008
this.C128_Code.CaseSensitive = true;
http://www.codeproject.com/Articles/20823/Barcode-Image-Generation-Library?display=Print 5/10
9/30/2014 Barcode Image Generation Library - CodeProject
etc.)
Bug fixed (thanks JackyWu2005) in Code128.cs that was preventing the proper start
characters from being inserted. This only happened when switching back to A from
any other encoding type.
December 3, 2008
Fixed bug in Code 128C that would not insert a 0 in front of an odd length string of
raw data.
Fixed a bug (thanks Shaihan Murshed) in Code 39 that let the user encode * in the
string of data. This should only be used for starting characters, and is now handled.
Fixed a bug in Code 39 that was inserting an extra 0 at the end of the encoded
string. This did not affect the validity of the barcodes, it was just not supposed to be
there.
Added a new property to the library called FormattedData. This value is what is
actually encoded. It is formatted for certain types of barcodes, for example, Code 39
requires * in front and behind the barcode.
Fixed a bug in Code128-A and Code128-B that would cause it to encode incorrectly
due to incorrectly trying to compact the barcode for Code128-C. This functionality is
now bypassed if Code128-A or Code128-B is selected.
Removed a useless variable bEncoded from BarcodeLib.cs.
static methods now support generating the data label (required addition of a
parameter to 3 of the 5 static methods used to encode).
Property now available to retrieve the amount of time (EncodingTime) it took to
encode and generate the image. (Might be helpful for diagnostics.)
Modified a few error messages to be more descriptive about correcting the problem
with data length.
The Barcode class now inherits from IDisposable - XML export functionality
added to BarcodeLib to allow the data, encoded data, and other properties to be
exported in XML along with the image in Base64String format. This includes
http://www.codeproject.com/Articles/20823/Barcode-Image-Generation-Library?display=Print 6/10
9/30/2014 Barcode Image Generation Library - CodeProject
Fixed a bug that allowed non-numeric data to be encoded with Code128-C, a check
has been put in place to handle this. It throws an error EC128-6 now, if found to
contain something that isn't in Code128-C.
Fixed a bug in GetEncoding() for C128. This would allow Code128-B to switch and
dynamically use Code128-A if it couldn't find a char in its set.
Changed the use of a Pen object that was not disposed of. This was not causing a
problem, just bad technique.
Fixed an encoding issue with C128-B that had a wrong character in its encoding set
at one point (U instead of Z in codeset B).
The UPC-A check digit is now calculated every time whether 11 or 12 digits are
passed in. If 12 is passed in and it has got an incorrect check digit, then it is
replaced with the correct check digit. This prevents an unscannable barcode from
being generated.
The EAN13 check digit is now calculated every time whether 12 or 13 digits are
passed in. If 13 is passed in and it has got an incorrect check digit, then it is
replaced with the correct check digit. This prevents an unscannable barcode from
being generated.
All errors can be accessed via the BarcodeLib.Errors properties which is a list of
separate errors encountered.
All symbologies were moved to the BarcodeLib.Symbologies namespace for
better organization.
The FormattedData property was not being used, so it was removed.
The Version property was added to BarcodeLib to allow reading the libraries
version number.
Fixed a bug in Code 39 extended that was erasing the start and stop characters if
extended was used.
Fixed a bug that if barcodes were aligned left or right, they would cut off a part of
the starting or ending bar, which was a drawing bug that's been present since
1.0.0.0.
Fixed a bug in Code 128C that checked for numeric data; if it was bigger than Int64
and was numeric, it would throw an exception saying it was non-numeric data.
Fixed a bug in UPC-A that encoded with the same sets as EAN-13 and only CodeA
http://www.codeproject.com/Articles/20823/Barcode-Image-Generation-Library?display=Print 7/10
9/30/2014 Barcode Image Generation Library - CodeProject
Fixed a bug in drawing of barcodes that caused barcodes to be cut off on the left
and right when aligned to the sides.
Fixed a bug in the project where the BarcodeXML dataset was corrupt.
Added the GetSizeOfImage function that returns the real world coordinates of the
EncodedImage.
Facing Identification Mark(FIM) symbology added.
Fixed a bug in the Codabar symbology that would not allow valid non-numeric
http://www.codeproject.com/Articles/20823/Barcode-Image-Generation-Library?display=Print 8/10
9/30/2014 Barcode Image Generation Library - CodeProject
Fixed a bug in Code 11 where K checksums were being calculated for messages
shorter than 10 characters in length.
Fixed a bug in PostNet that drew incorrectly for all PostNet barcodes (thanks
jonney3099)
Added Code 39 Mod 43 support
Updated the GetImageSize method to return an ImageSize object containing the
real world size of the image generated.
License
This article, along with any associated source code and files, is licensed under The Code Project
Open License (CPOL)
Share
Brad Barnhill
Software Developer Cerner Corporation
United States
Brad Barnhill has a Masters in Nursing from the University of Tennessee (UTHSC), and a Bachelors
http://www.codeproject.com/Articles/20823/Barcode-Image-Generation-Library?display=Print 9/10
9/30/2014 Barcode Image Generation Library - CodeProject
in Computer Science from UT as well. His interests are C#, barcodes, developing controls for
other developers to use, and distributed computing. He has been employed as a software
developer since 2004.
http://www.codeproject.com/Articles/20823/Barcode-Image-Generation-Library?display=Print 10/10