|
|
|
HOW TO CONTROL BARCODES WITH CODE USING THE GDI+ PART OF THE .NET FRAMEWORK |
|
| Introduction | |
|
[ Back To Top ]
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 | |
|
[ Back To Top ]
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: |
|
| The Main Flow | |
|
[ Back To Top ]
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
Printing a Barcode [ Back To Top ]
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 SubWe 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 [ Back To Top ]
I feel this solution is a good one since relying on a third party's font, component, or control can sometimes be troublesome in deployment or 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.