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

This author has published 5 articles on DotNetSlackers. View other articles or the complete profile here.

Other articles in this category


Introduction to 3-Tier Architecture
Brian Mains explains the benefits of a 3-tier architecture.
Settings Manager for Windows Vista Sidebar Gadgets
SettingsManager is a JavaScript library that allows Windows Vista Sidebar gadgets to persist common ...
ORM in .NET 3.5
This article covers a general introduction to ORM concepts, the approach that .NET 3.5 takes, and ho...
Delegates in .NET
Rupesh Kumar Nayak explains delegates in .NET.
SuperToolTip
Office 2007 offers great new features, one of them is the SuperTooltip which provides much more info...

You might also be interested in the following related blog posts


Deep Zoom Image Generation with DeepZoomTools.DLL read more
Image Transforms with the ASP.NET Generated Image Control read more
ASP.NET AJAX Release History : Q2 2009 BETA (version 2009.2.616) read more
Silverlight Server Configuration read more
Are you caching your images and scripts? IIS SEO can tell you read more
RADactive I-Load 2008.R1 released - Wysiwyg image upload-resize-crop tool read more
Aspose.Total for .NET Q4 2008 Released read more
Getting Images to render properly in the ASP.NET Designer read more
ImageDraw 4.0 for ASP.NET now available read more
Barcode Professional 5.0 for .NET now available read more
Top
 
 
 

Discussion


Subject Author Date
placeholder thank you Ahmed Helmy 7/1/2008 4:44 AM
:: Good One :: Gemini Gemini 2/6/2009 12:56 AM

Please login to rate or to leave a comment.

Product Spotlight