Barcode image generation made easy

Published: 25 Jun 2007
By: Imran Nathani

In this article we are going to generate barcodes in .NET. Barcode-aware devices, such as scanners and printers, are readily available on the market, as long as the barcodes we generate follow the standards.We will implement barcode generation using Microsoft Ajax in a web application.

Introduction

To most of us, barcodes are a common sight. This is because today barcodes enable delivery and retail systems to perform faster. When programming with older languages, implementing barcodes is difficult. It would need either a set of computer graphics programmers with knowledge of different barcodes standards, or an expensive third party component. But things are different today. With the help of the .NET framework, we will perform the same task in an ASP.NET page with just a few lines of code.

Selecting the Barcode Standard

Barcodes are a pictorial representation of a particular set of information. This information is conveyed through dark colored (normally black) lines of different widths, on any light (normally white) background. For uniform implementation, we have few standards of barcodes, like EAN/UPC, Code 39, Code 128 and so on. Without really understanding the theory behind these standards we just have to decide, as per our requirement, to which standard we want to adhere. With the code presented in this article, any standard can be implemented.

Bar-coding or fonting?

If we search the Internet we can easily come across free font files which implement most barcode standards. Using these font files, we can draw the information string on an image and set the font of that string to the barcode font. This actually reduces the whole task of information bar-coding to simply information fonting. In our example, we will use the file Barcodefont.ttf.

Before we begin

We are going to split the whole procedure into two parts. The first one involves generating the image. The second one discusses image delivery. The image generation consists of drawing the barcode on a canvas and storing it in a file. Regarding image delivery, we consider two options: downloading the image or displaying it directly in a control.

Every font file (.ttf) has a typeface name. Since we need to use it in our code, let’s open the font file and check the typeface name as shown in figure 1.

Figure 1: Checking the typeface name

Also, we need to decide a way to provide a unique name to each file that is generated by different users, in order to avoid resource conflict. In the code, we use the following filename format:

[text to be bar-coded]_[long representation of DateTime.Now].jpg

Therefore, a conflict would arise only if two users try to barcode the same string at the same instant (up to milliseconds).This conflict would be resolved in the next millisecond.

Barcode Image Generation

To generate the image, we first need to import some namespaces related to drawing and file management.The code for generating the image is reported in listing 1.

Listing 1: Code for generating the image

Barcode Image Delivery

Image delivery is about how we want our users to get the generated image. There can be multiple complex ways for image delivery; involving hyperlinks, databases, zipping and so on. We will consider two options:

1) Displaying the image in a control

Here we would display the image directly in an image box on a different page. The path to access the image would be that of an .aspx page with the text to be bar-coded appended to the the query string, like so:

2) Downloading the image.

In this case, we would directly send the file for download by the user. The user actually saves the image file to their hard disk, as shown in the following code:

Cleaning up the code

Despite which image delivery option we choose, an image file is generated on the server. If this file is required in the future, then the following clean up code is not required. However, if these files are not needed, we can write the clean up code just before disposing the response object, like so:

Summary

A wise man once said "Knowledge is my weapon”. This is evident in this case, because of the price of third party bar-coding components on the Internet. For more information on barcodes and barcode standards, check out this Wikipedia entry.

About Imran Nathani

Sorry, no bio is available

View complete profile

Top Articles in this category

Settings Manager for Windows Vista Sidebar Gadgets
SettingsManager is a JavaScript library that allows Windows Vista Sidebar gadgets to persist common settings that all gadget instances have access to.

SuperToolTip
Office 2007 offers great new features, one of them is the SuperTooltip which provides much more information about controls than standard old style tooltips. This article shows how to build tooltips in such a way.

ORM in .NET 3.5
This article covers a general introduction to ORM concepts, the approach that .NET 3.5 takes, and how it compares to these other packages.

Introduction to 3-Tier Architecture
Brian Mains explains the benefits of a 3-tier architecture.

Foundations of Programming - Part 3 Persistence
The Foundations of Programming series looks at a number of key concepts, techniques and tools specifically designed to help developers meet the growing complexity of enterprise systems. Based on proven principals like unit testing, domain driven design, dependency injection and O/R Mappers, the series is aimed at developers interested in helping themselves.

Top
 
 
 

Discussion


Subject Author Date
placeholder thank you Ahmed Helmy 7/1/2008 4:44 AM

Please login to rate or to leave a comment.

Product Spotlight