Total votes: 0
Print: Print Article
Please login to rate or to leave a comment.
Published: 25 Jun 2007
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.
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
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
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
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:
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
Sorry, no bio is available
This author has published 5 articles on DotNetSlackers. View other articles or the complete profile here.
Please login to rate or to leave a comment.