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 imageAs 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.
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 formatsThere 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.
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.NETA 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.
Stream to Web
With the code above we can access the barcode via a URL — something like this: