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
This author has published 5 articles on DotNetSlackers. View other articles or the complete profile here.
|
You might also be interested in the following related blog posts
ASP.NET AJAX Release History : Q2 2009 BETA (version 2009.2.616)
read more
Are you caching your images and scripts? IIS SEO can tell you
read more
Image Transforms with the ASP.NET Generated Image Control
read more
Aspose.Total for .NET Q4 2008 Released
read more
Deep Zoom Image Generation with DeepZoomTools.DLL
read more
ImageDraw 4.0 for ASP.NET now available
read more
Silverlight Server Configuration
read more
RADactive I-Load 2008.R1 released - Wysiwyg image upload-resize-crop tool
read more
Barcode Professional 5.0 for .NET now available
read more
Getting Images to render properly in the ASP.NET Designer
read more
|
|
Please login to rate or to leave a comment.