Welcome to Professional ASP.NET - Chris Love's Official Blog Sign in | Join | Help

Chris Love's Official ASP.NET Blog

Chris Love's Helpful tips, tricks and pragmatic development knowledge for the ASP.NET world.
Add to Technorati Favorites


ASP Insider
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!

Posted: Sunday, November 09, 2008 4:02 PM

by Chris Love
Filed under: ,

Comments

DotNetKicks.com said:

You've been kicked (a good thing) - Trackback from DotNetKicks.com

# November 9, 2008 4:06 PM

Khalid Abuhakmeh said:

That's pretty cool. Short and sweet, I like it.
# November 10, 2008 8:37 AM

roarkweb said:

Just what I needed! Thanks for the example.
# November 19, 2008 3:18 PM

dora said:

Thanks for the info. Other barcode generator site: http://barcodez.net/
# December 9, 2008 11:46 PM
Leave a Comment

(required) 

(required) 

(optional)

(required) 

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS