Published:
24 Aug 2005
|
Abstract
Barcode components for .NET can be very expensive. Would you like a free alternative? Terry Voss provides a simple and extensible barcode solution that creates barcode graphics using only the .NET Framework. |
 |
by Terry Voss
Feedback
|
Average Rating:  
Views (Total / Last 10 Days):
24234/
42
|
|
|
Introduction |
This article provides a barcode solution that does not use any components, controls, or fonts. The barcodes are drawn with code using the classes of the System.Drawing namespace, which exposes the GDI+ subsystem of Windows. The solution shows how to create and display Code39 and UPC-A barcodes on an ASP.NET web page. The code is simple to extend to support other barcode formats, once you have found the defining characteristics of the barcode format. Ease of testing, debugging, and feature change will be shown to be attributes of this solution. |
The Source Code |
In the code dowload, gdibarcodes.zip, there are four files: webform1.aspx, webform1.aspx.vb, barcode.aspx, and barcode.aspx.vb. The webform1.aspx page has one image control on it with the following ImageUrl property: code=694038220006&height=14&width=1&mode=code39&text=1 This ImageUrl property means that the image is making a request to IIS for the page barcode.aspx with five query string parameters. Barcode.aspx has no controls, just code in its codebehind. Webform1.aspx.vb has no code. So all of the functional code is located in the file, barcode.aspx.vb. |
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 |
Printing a Barcode |
Pass the Graphics type instance, graph, into your procedure so you can paint on it. Create some objects to hold positioning. Origy and EndY will be constant over the printing, but curX is the main thing that will vary to make the barcode’s lines. We are printing or not printing every pixel, and since the pixel is the default unit of the image, this works nicely. Now we will get rid of any asterisks (*) in our code since they should not be used in the text code ever, only on the ends. We loop through the letters of the code now including the asterisks.
Code Listing 2 – Printing the Code39 Barcode
Private Sub PrintCode39(ByVal graph As Graphics)
Dim rHeight As Integer = _height - 7
Dim curPattern As String
Dim origx As Integer = 4
Dim origy As Integer = 2
Dim endY As Integer = origy + rHeight
Dim curX As Integer
curX = origx
Dim pattern As String
Dim code As String = "*" & Replace(_code, "*", "") & "*"
For i As Integer = 0 To code.Length - 1
pattern = Me.GetC39Pattern(code.Substring(i, 1))
For Each patt As Char In pattern
If patt = "1"c Then
graph.DrawLine(Pens.Black, curX, origy, curX, endY)
curX += 1
Else
curX += 1
End If
Next
Next
End Sub
We want to print the pattern associated with each letter of the code. To get the code is different for each barcode. For Code39 it is a simple matter of a select statement to get the right pattern. For UPC-A, notice that although we only print numbers so there are only nine digits to be concerned with, there are separate sets of patterns for the left six digits, versus the right six digits (UPC-A always has 12 digits in the code).
Printing the barcode pattern is a simple matter of looping through each character of the code. If the first char type in the pattern is “1”c then print, otherwise just update the curX positon object to leave a blank space in your barcode’s printed pattern. The UPC-A has fixed constant patterns at front, middle, and end called “Guard bars” to make the barcode easier to read, so those parts are constant printed patterns. Notice though that the same pattern loop is used for both barcode formats.
Image 1 – The Barcodes

Reference
I figured out how to do the UPC-A code by reading the information titled Encyclopedia: Universal Bar Code. |
Summary |
I feel this solution is a good one since relying on a third party's font, component, or control can sometimes be troublesome when you want to debug or to change some features regarding your application. This solution is very easy to test by just printing your webpage and scanning the code to make sure it works. You can trace the code and look at each pattern easily, so debugging is a dream. Changing features with such simple code is straight forward. |
|
|
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
|
|