A Quick and Dirty Bar Code Image httpHandler

Yesterday I was trolling the ASP.NET forums killing some time and answering questions when I came across a thread where someone was seeking some help on a FREE .NET Bar Code library. Last summer I needed the same thing and was working on manipulating PDF files at the same time and worked with the iTextSharp library which does both, so I recommended it to the seeker. I checked back this afternoon and see he has found it to meet his needs, but I saw his implementation of the Bar Code image generator was using a Page class. If you know me you know I said, ugh. This should be an httpHandler! So I spent 5 minutes and created a quick and dirty Bar Code Generating httpHandler to get things started.

All the bar code generation is done in the iTextSharp bar code class itself, the httpHandler just serves it up.

Public

Sub

ProcessRequest(

ByVal

context

As

System.Web.HttpContext)

Implements

System.Web.IHttpHandler.ProcessRequest

Dim

Request

As

HttpRequest = context.Request

Dim

response

As

HttpResponse = context.Response

Dim

bc39

As

New

Barcode39()

If

Not

IsNothing(Request(

"code"

))

AndAlso

IsNumeric(Request(

"code"

))

Then

bc39.Code = Request(

"code"

)

Else

bc39.Code = Now.Month.ToString & Now.Day.ToString & Now.Year.ToString

End

If

Dim

bc

As

System.Drawing.Image = bc39.CreateDrawingImage(System.Drawing.Color.Black, System.Drawing.Color.White) response.ContentType =

"image/gif"

bc.Save(response.OutputStream, System.Drawing.Imaging.ImageFormat.Gif)

End

Sub

Of course with a custom httpHandler (not a generic handler, which would work just as well) it must be registered in the web.config file.

<add verb=

"GET"

path=

"barcode.gif"

validate=

"false"

type=

"BarCodeHandler"

/>

Finally an image needs to reference the barcode.gif file that was just mapped. It needs to be in a web page. Notice in the httpHandler code I have it check for a Code variable, if it does not exist then I just use the numerical values of the current date.

<

img

src

="barcode.gif?code=1234567890"

/>

Voila, a quick and dirty Bar Code image generating httpHandler!

Get the Source Code!

Share This Article With Your Friends!

Googles Ads Facebook Pixel Bing Pixel LinkedIn Pixel