Using GDI+ to Draw Barcodes
page 4 of 5
by Terry Voss
Average Rating: 
Views (Total / Last 10 Days): 24235/ 43

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
          curX += 1
        End If
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


I figured out how to do the UPC-A code by reading the information titled Encyclopedia: Universal Bar Code.

View Entire Article

User Comments

Title: 123   
Name: 123
Date: 2013-01-15 8:33:43 AM
Title: linear barcode   
Name: andbar
Date: 2012-05-28 3:54:45 AM
linear barcode generator:
Title: Thanks, good article   
Name: Richard
Date: 2008-09-07 11:36:44 PM
Thanks, good article
Title: is it possible   
Name: Lanumin
Date: 2008-03-10 8:50:16 AM
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
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

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.

Title: Letters not printing   
Name: Terry Voss
Date: 2007-06-27 12:04:49 PM
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

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
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
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 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 2.0 VS 2005   
Name: Isidore
Date: 2007-06-12 6:35:23 PM

I appreciate your solution...However, I tried running the code on VS 2005 ( 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
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,

Title: Barcode   
Name: Amit
Date: 2006-10-17 9:24:46 AM
this is really nice concept to gererate barcode..
Title: Code 128   
Name: Ketan
Date: 2006-04-17 1:52:17 AM
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
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
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
Title: great   
Name: Qqczka
Date: 2005-09-13 5:33:54 PM
Works great on VB.NET Windows.Forms... with some changes (
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
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-2023  |  Page Processed at 2023-12-07 6:57:26 PM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search