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

Microsoft Store

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

Reyaz said:

Hi Chris, Thanks alot for the code, it is working great and easy to add .
# October 18, 2009 6:00 PM

Reyaz said:

Hi Chris, Thanks for sharing the code, the code works fine on the localhost. but when i use it on the server it displays only an empty image. is there any steps to be followed to make it working on the web server. I have already installed acrobat reader on the server. Thanks for the help
# October 23, 2009 6:33 AM

Sobot said:

Hi Do you know how to generate the barcode with the text of the code on the bottom?
# December 25, 2009 12:17 PM

Abhishek said:

I can Not use it in my application in C# i don't have knowledge of Handler plz response abhisheksoni85@gmail.com
# March 5, 2010 7:35 AM

Gavin said:

Hi, Great article, many thanks. I just wanted to put a small update for those running IIS7, if everything works fine when running locally in VS debug mode, but you get a red x when accessing it remotly, you may need to add the handler in the section as well as/or the i.e.
# June 11, 2010 9:23 AM

Prakash said:

I am getting following error message , Any idea on how to add the trust level to allow the page Security Exception Description: The application attempted to perform an operation not allowed by the security policy. To grant this application the required permission please contact your system administrator or change the application's trust level in the configuration file. Exception Details: System.Security.SecurityException: That assembly does not allow partially trusted callers. Source Error: Line 19: bc.Save(Response.OutputStream, System.Drawing.Imaging.ImageFormat.Gif); Line 20: Line 21: } Line 22: } Source File: d:\hosting\5875909\html\royalexpress\Test\barcode.aspx.cs Line: 21
# July 10, 2010 7:15 AM

Md. Golam rabbani said:

How can i add the bar code?
# July 15, 2010 7:58 AM

Panda MX said:

Buen ejemplo, basico y practico, Gracias !!
# October 14, 2010 1:55 PM

Eduardo P&#225;ez said:

Hi! This solution is great!! I tried your code in mi local server and it worked just fine. But when I uploaded it to the real server I keep getting an error message "BC30002: Type 'Barcode39' is not defined.". Do I missed something? Why it's not getting it!! Thanks!!
# June 8, 2011 4:02 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