Using GDI+ to Draw Barcodes
page 3 of 5
by Terry Voss
Feedback
Average Rating: 
Views (Total / Last 10 Days): 24319/ 60

The Main Flow

When the webform1.aspx page loads in the browser, part of its loading is to request each external image file. The image control makes a request to the barcode.aspx page and its Page_Load event handler is executed.  I am not using the width and height variables much, just to get the image wide enough for the barcodes I’m using; but you could make them work proportionally, or you could use a transform to cause scaling of your barcodes as a feature addition. After obtaining the parameter values, I create an instance of the Bitmap type by specifying a width and height. The bitmap is what will be sent back to the requesting image control for display. Then I create an instance named graph that is of type Graphics using the bitmap.

I then print the proper barcode and, if text query string value is 1, I print the text underneath it. (Notice that with UPC-A it is standard to print the text of the code overlapping the bottom of the barcode graphic, so I used two filled rectangles to do that and it helped me to color them lawngreen at first to get them lined up with the textcode. These rectangles are necessary to block the graph where the textcode will print.) Lastly, since the request is from the image control, not the whole webform1.aspx page, I save the bitmap to the Response.OutputStream in the proper GIF format.

Code Listing 1 – Page Load Event

Try
  _code = Request.QueryString.Item("code")
  _height = CType(Request.QueryString.Item("height"), Integer)
  _mode = Request.QueryString.Item("mode").ToLower
  _text = CType(Request.QueryString.Item("text"), Integer)
  _height = _height * 2 : _width = 105 


  If Not _text = 0 And Not _text = 1 Then
    Throw New Exception("_text is out of range = 0-1")
  End If


  Dim bcBitMap As Bitmap


  If _mode.ToLower = "upc-a" Then
     bcBitMap = New Bitmap(_width, _height)
  Else
     bcBitMap = New Bitmap(_width * 2 + 20, _height + 10) 
  End If


  Dim graph As Graphics = Graphics.FromImage(bcBitMap)
  graph.Clear(Color.White)
  Me.PrintBarCode(graph, _mode)


  If _text = 1 Then
     If _mode.ToLower = "upc-a" Then
        Me.PrintUpcText(graph, _code)
     Else
        Me.PrintC39Text(graph, "*" & _code.Replace("*", "") & "*")
     End If
  End If


  Try
     Response.ContentType = "image/gif"
     bcBitMap.Save(Response.OutputStream, ImageFormat.Gif)
  Catch ex As Exception
     Dim errorMsg As String = ex.message
  End Try


Catch ex As Exception
   Dim errorMsg As String = ex.Message
End Try


View Entire Article

User Comments

Title: 123   
Name: 123
Date: 2013-01-15 8:33:43 AM
Comment:
123
Title: linear barcode   
Name: andbar
Date: 2012-05-28 3:54:45 AM
Comment:
linear barcode generator:
http://www.keepdynamic.com/1D-2D/linear-barcode-generator-net.shtml
Title: Thanks, good article   
Name: Richard
Date: 2008-09-07 11:36:44 PM
Comment:
Thanks, good article
Title: is it possible   
Name: Lanumin
Date: 2008-03-10 8:50:16 AM
Comment:
Is there a way to have a working barcode tatoo?
Title: upc converted to private product code   
Name: davidp.
Date: 2007-07-20 1:50:27 PM
Comment:
I have a question. How can i scan a upc barcode and use that for my 5 digit company item number on a barcode? Ok when we receive items in we make our own labels for these items without using their upc codes that are already on these items. we have a description of the item on file and it is converted to our 5 digit barcode for inventory control.
Title: Everything is good now...!   
Name: Isidore
Date: 2007-06-27 2:29:58 PM
Comment:
Hello,

I called the scanner manufacturers an they told me that some parts of the the barcode were not printed. I increased the size of the image to allow for no cutting of letters. It worked perfectly.

This is only possible with Code39 though as I realized from the article you referenced that UPC-A supports digits...(not verified)

I have printed all the characters in any length now using code39 and they all scanned well.

Thanks for the free code once again.

Isidore.
Title: Letters not printing   
Name: Terry Voss
Date: 2007-06-27 12:04:49 PM
Comment:
Place a breakpoint on the line of code:
if _text = 1 then and use F11 to trace through the code for creating the print of the letters to see why it is not printing.
Title: UPC Code for letters (a,b,c...) printed 0000000000   
Name: Isidore
Date: 2007-06-27 7:56:10 AM
Comment:
Hello,

Thanks for the code. I recently tested the barcodes with a scanner and the upc worked fine. However, the letters did not print. They generated 0's.

Is there anything that could be done about this. Urgent response will be highly appreciated.
Title: Thanks   
Name: Isidore
Date: 2007-06-14 11:37:32 AM
Comment:
Thanks for your advice. I added a button to the barcode.aspx page to raise the event placed on the page load event handler. This way I was able to confirm that the code is valid. I will figure out how to place the barcodes on pages of production control documents to be used by scanners.

Using a scanner to read these barcodes is the ultimate feature of the system I am building. If you have any help in this regard...I will appreciate it a lot.

Thanks for your prompt response though.
Title: bcode.barcode not found   
Name: Terry Voss
Date: 2007-06-13 12:18:04 PM
Comment:
you see there is a public class barcode defined in one of the 4 source files, right? So the culprit is the bcode part which is: a namespace default for the vb asp.net project of the name bcode. So you need to name your project bcode, or change the namespace part of the object instance specification: bcode.barcode to myprojectname.barcode.
Title: Unable to run code on asp.net 2.0 VS 2005   
Name: Isidore
Date: 2007-06-12 6:35:23 PM
Comment:
Hello,

I appreciate your solution...However, I tried running the code on VS 2005 (asp.net 2.0) and here is the error message:
"Could not load type 'bcode.barcode'"

I tried removing bcode but it still did not work. Urgent response will be highy appreciated as I see that most of your fans really like the code and it seems to be working for them.
Title: Equivalent for C#   
Name: pedro
Date: 2006-10-24 8:22:27 PM
Comment:
First of all, I want to thank you for this code.
Second, I want to know if anyone has already tried to convert sucessfully this code to C#?

Thanks in advance,

pedro
Title: Barcode   
Name: Amit
Date: 2006-10-17 9:24:46 AM
Comment:
this is really nice concept to gererate barcode..
Title: Code 128   
Name: Ketan
Date: 2006-04-17 1:52:17 AM
Comment:
Hello, can someone say what configurations are required to print Code 128 barcode.
Title: not printing the last *   
Name: Roel
Date: 2005-11-04 5:06:49 AM
Comment:
Hi,
I'm having the following problem. I tried the code as found in the zipfile, but the last * is not printed correct in the barcode. All the other numbers and * are printed correct.

Can somebody give me some advice ??

Thx !
Title: somequestion   
Name: tarek
Date: 2005-11-02 9:03:44 AM
Comment:
i am very greatful to reply i working on visual c++6 and
i tried to make a barcode by using visual c++6 by using PrintBarcode and some function but i have a failer to do this ..can you help me and send for me complete example to create a barcode for any type
thank you for your helping
tarek taha
system analyst
ministory of heath
saudia arabia
tarek13051971@yahoo.com
Title: great   
Name: Qqczka
Date: 2005-09-13 5:33:54 PM
Comment:
Works great on VB.NET Windows.Forms... with some changes (http://www.qqczka.prv.pl/testBarCode.zip).
Somebody know another algorithm for EAN-128 ???
Like C-39 in this article:
Case "9" : clet = "1011100010111010"
Case "0" : clet = "1010001110111010"
Case "A" : clet = "1110101000101110"

Lukas (Poland)
Title: Thanks   
Name: T
Date: 2005-09-10 5:10:02 PM
Comment:
Thanks for this solution. It's an outstanding alternative to using those components and installing fonts on the server.

Keep it up!

Product Spotlight
Product Spotlight 





Community Advice: ASP | SQL | XML | Regular Expressions | Windows


©Copyright 1998-2022 ASPAlliance.com  |  Page Processed at 2022-10-01 10:53:15 AM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search