A very common requirement for applications is the need to print out a barcode using Crystal Reports or some other medium so that users can track products, inventory etc.  The following tutorial should get you from 0-60 in about 4.5 seconds.

Choose your Barcode

All of the following Barcodes will be easily read by your scanner.

Alphanumeric

Code 39 (3 of 9)- Probably the simplest to use bar code that allows variable lengths.  Text must be surrounded by an "*" aka "*RUNXC123*" for bar code readers to use it.

Code 128 - Recommended for general use code 128 includes a checksum and is much more compact than Code 39.

Numeric

Code 25 - A very simple numeric bar code of variable length.

Others. there are plenty of other bar codes and if you need to know more about them here is a Wikipedia article on them.

Fonts or Images

Fonts-  In an ideal world I think that the use of fonts would be the preferred method but there are a couple of downsides with using fonts.  

1. The font you decide to use will most likely need to be installed on !EVERY! computer that uses the report.

2.Even if you use a font you will still need to encode the text used in your barcode.  While this is easy using Code 39 most other barcodes require a checksum which must be calculated and added as characters that match the checksum.

3.Fonts cost money!  They seem quiet simple and you can find a font called "FREE 3 of 9" but it is difficult to find any other free TrueType fonts for other barcodes like UPC, Code 25 or Code 128.

Images-

Images solve most all of the problems associated with using fonts but they do pose one problem.  Images are much more fixed in size.  Using most Open Source barcode libraries you can make the image larger by adjusting the barcode weight but you can not make the image smaller. 

Lessons Learned

In my situation I decided to use an image and Barcode 128 as some of my barcodes were 20 characters in length.

Resizing the image will most likely result in it being un-readable.  I tried resizing the image(which was already using the minimum barcode weight of 1) in Crystal Reports as well as with C#.   If you have been able to resize an image by 50% and were able to still read it I would like to see your code as I tried 4-5 ways of doing it with unfavorable results each time.

 

Code Example

DataTable dt = new DataTable("BarLabel");
dt.Load(reader);

dt.Columns.Add("image", System.Type.GetType("System.Byte[]"));
foreach (DataRow dr in dt.Rows)
{
    string text = dr[0].ToString();
    using (MemoryStream ms = new MemoryStream())
    {
        Code128BarcodeDraw drw = BarcodeDrawFactory.Code128WithChecksum;
        using (Image myimg = GenCode128.Code128Rendering.MakeBarcodeImage(text, 1, true))
        {
            myimg.Save(ms, System.Drawing.Imaging.ImageFormat.Bmp);
            dr["image"] = ms.ToArray();
        }
    }

}

crystalReport.Database.Tables[0].SetDataSource(dt);

 

Extra Info

In the example above I am using the Open Source code found in this article on CodeProject.  If you need to use a different barcode I suggest using either the Barcode Rendering Framework found on CodePlex http://barcoderender.codeplex.com/ or using ItextSharp.   I used all three and preferred the code from CodeProject though it only implements Code 128 where as the other libraries implement most all barcode symbols.

If you use the code snippet above your image will come across in Crystal Reports as a blob field.  You must set the property of "Can Grow" or else you will find Crystal Reports adjusting the size of your image which will make it unreadable. 

Oh and if the article helped you vote it up and try visiting a sponsor.

Submit this story to DotNetKicksShout it   Bookmark and Share