TechRepublic : A ZDNet Tech Community

How Do I...

Host: Mark Kaelin
Contact

How do I... Generate barcodes using C#?

There are many different kinds of barcodes. The format we’re dealing with here is called “3 of 9″ (also referred to as Code 39). Most all third-party component libraries can generate these codes — but there are also other options for generating 3 of 9 barcodes. The option we’re going to use for this How do I is a free font created by Matthew Welch called “Free 3 of 9.” This font is 100 percent free, you pay nothing, and you’re even allowed to use it in commercial applications!

This blog post is also available in PDF form in a TechRepublic download.

Using Free 3 of 9

We are going to use the Free 3 of 9 font to in conjunction with the .NET Framework’s GDI functionality to render the data presented by the font into a bitmap. Once we have that bitmap we can manipulate it in various ways until it is the size/shape we want. First you will need to download Free 3 of 9.

After you’ve downloaded the zip file extract the two font files contained within onto your desktop. From there drag and drop the files into your font directory. For XP, Vista, and 2003 server this directory is “C:\WINDOWS\Fonts\”. As you move the font files into the font directory the fonts are automatically installed. To test that the font is installed open notepad and see if you can change the font to Free 3 of 9.

Creating the barcode image

As mentioned above, we’ll be writing the barcode out onto a bitmap using GDI. The code in Listing A shows a function which takes a string as a parameter and returns a bitmap.

Listing A

Create a barcode

There are a couple points to make here:

  • We have a kind of chicken and the egg problem with our Graphics and Bitmap objects. We need the Graphics object to determine the size of our Bitmap, and we need the Bitmap to get the Graphics object! This seems unclean but the performance isn’t really affected by it. The code shown generates well over 500 barcodes per second.
  • We’re setting the background of the image to white in this example, but you can also set the background to transparent or any other color.
  • We use the SingleBitPerPixel TextRenderingHint. If you use a different hint (ie AntiAlias) you risk having the barcode come out blurry, and that could render the code unreadable. Using SingleBitPerPixel forces the font to be rendered with sharp edges.
  • We use a font size of 60. It is important that our barcode is large enough to be clearly read. The size is up to you though, and you may be able to get away with using a smaller font.
  • The Free 3 of 9 font uses the asterisk character as a special character that symbolizes the beginning and end of the barcode. You’ll need to wrap the data you pass to the CreateBarcode function in asterisks, or modify the CreateBarcode method to do this automatically.

Converting the barcode to other image formats

There are times when you need a barcode to be presented as either a JPEG or GIF — one instance is when customers will be printing the codes from your Web site. Doing this is easy when you use the CreateBarcode function as a base — the code to convert the returned bitmap to a GIF is shown in Listing B.

Listing B

Convert to .GIF

To convert the bitmap to a JPEG simply replace ImageFormat.Gif with ImageFormat.Jpeg and change the file extension to “.jpg”.

Streaming barcodes from ASP.NET

A really interesting side effect of being able to convert the bitmap to a GIF or JPEG is that we can use that functionality and stream the image from an ASP.NET page. This allows you to dynamically generate barcode images that can be embedded in a Web page. To do this simply create a normal ASP.NET Web Form (.aspx), and enter the code shown in Listing C into the Page_Load event.

Listing C

Stream to Web

With the code above we can access the barcode via a URL — something like this:

http://yourwebsite/BarcodeGenerator.aspx?data=11111111

Print/View all Posts Comments on this blog

Link to code curtis.cook@... | 11/27/07
Re: Link to code zs_box@... | 11/28/07
EAN-13 eric.drapier@... | 12/03/07
RE: How do I... Generate barcodes using C kryan@... | 11/29/07
Public Domain Code 128 font eric.drapier@... | 11/29/07
This was an excellent resource! milind.a.joshi@... | 11/29/07
Thanks for the info kryan@... | 11/30/07
RE: How do I... Generate barcodes using vb6 jonrmoore@... | 11/29/07
Use the same font AbbydonKrafts | 11/30/07
good MICR font? aikimark@... | 11/29/07
How about a free one computer-ease@... | 11/29/07
TT8731Z_.TTF works for us yankeek@... | 12/06/07
what is the source of the font? aikimark@... | 12/07/07
how do i generate barcodes using C kvsrk2006@... | 11/29/07
how nice trebuntou@... | 12/01/07
Practical Application of this sample code mjg@... | 05/31/09
RE: How do I... Generate barcodes using C heartbeats_111 | 01/24/10
Huh ? Tony Hopkinson | 01/24/10

What do you think?

White Papers, Webcasts, and Downloads

Recent Entries

TR on Twitter

Top Rated

    Archives

    TechRepublic Blogs



    500 Things Every Technology Professional Needs to Know
    Did you know Microsoft's RegClean does not work with XP but you can use shareware to clean your registry? Did you know most wireless access points don't have encryption enabled by default? Did you know there are 500 tidbits of information contained in TechRepublic's 500 Things Every Technology Professional Needs to Know that will help you become a successful IT professional.
    Buy Now
    IT Professional's Guide to Policies and Procedures, Third Ed
    Whether you're creating policies for management, training, personnel, support, privacy, Internet/e-mail usage, security, or inventory, you'll meet the needs of your entire enterprise with this one download!
    Buy Now

    SmartPlanet

    Click Here