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

AdyBoy said:

How view this result into CrystalReport file export to PDF. Thanks
# March 11, 2009 5:15 AM

rockfer said:

Great tutorial! I have a question though. How would you link the barcode image to a database? Or how did you store the information related to the Barcode? Thanks for the help!
# March 30, 2009 11:28 AM

Chris Love said:

AdyBoy, sorry I would not have a clue. I have not done any report work in 13 years.

Rockfer, You would need to change the creation of the barcode from the QueryString parameter to a value you retrieve from the database. Just like setting the Text property of a TextBox.

# April 3, 2009 9:24 AM

Gracie said:

Hi Chris, I tried working on your solution in generating barcode and it works fine in my page when I run it in localhost. But when I transferred the files in a server, it didn't generate the barcode. I wonder what's wrong. Have you tried it in a server? Thanks.
# April 4, 2009 10:38 PM

MH said:

Gracie - have you made sure the dependencies are on the server - I believe that iTextSharp needs acrobat reader installed
# April 28, 2009 6:21 AM
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