Click here to Skip to main content
15,867,704 members
Articles / Multimedia / GDI+
Article

Barcode .NET Control

Rate me:
Please Sign up or sign in to vote.
4.82/5 (64 votes)
10 May 20053 min read 418.7K   47K   193   67
A Code 39 barcode control with printing and saving support.

Sample Image - barcodectl.jpg

Introduction

I decided to create this control for a few reasons. First off, it seems that for some reason people think that barcodes are very complex, but they really aren't. I needed to display a barcode in an application and it seemed that all I could find were fonts, which I didn't want to install, or over-priced controls. Well, here you go, I hope that this control fits your needs, it is a simple code 39 barcode display which supports a header and footer, printing, saving, and is pretty well customizable.

Background

Code 39 (also known as "Code 3 from 9") is a discrete barcode, a fixed pattern of bars represents a character. Each character is made up of 9 bars, 3 of the bars are wider than the others. Each character is displayed as 5 black bars and 4 white bars. Code 39 supports 43 characters plus an additional character used as a start/stop character. The start/stop character in human readable form is the '*' character. The following is a list of the supported characters and their code 39 representation:

C#
String [] coded39Char =
{
    /* 0 */ "000110100",
    /* 1 */ "100100001",
    /* 2 */ "001100001",
    /* 3 */ "101100000",
    /* 4 */ "000110001",
    /* 5 */ "100110000",
    /* 6 */ "001110000",
    /* 7 */ "000100101",
    /* 8 */ "100100100",
    /* 9 */ "001100100",
    /* A */ "100001001",
    /* B */ "001001001",
    /* C */ "101001000",
    /* D */ "000011001",
    /* E */ "100011000",
    /* F */ "001011000",
    /* G */ "000001101",
    /* H */ "100001100",
    /* I */ "001001100",
    /* J */ "000011100",
    /* K */ "100000011",
    /* L */ "001000011",
    /* M */ "101000010",
    /* N */ "000010011",
    /* O */ "100010010",
    /* P */ "001010010",
    /* Q */ "000000111",
    /* R */ "100000110",
    /* S */ "001000110",
    /* T */ "000010110",
    /* U */ "110000001",
    /* V */ "011000001",
    /* W */ "111000000",
    /* X */ "010010001",
    /* Y */ "110010000",
    /* Z */ "011010000",
    /* - */ "010000101",
    /* . */ "110000100",
    /*' '*/ "011000100",
    /* $ */ "010101000",
    /* / */ "010100010",
    /* + */ "010001010",
    /* % */ "000101010",
    /* * */ "010010100"
};

Note that the representation as 1s and 0s is to specify the size of the bars. A 0 represents a single width bar and a 1 represents a double width bar. Each character will have 3 double width bars at a different location than the other characters.

Features

The control inherits from System.Windows.Forms.Control and the appearance of the barcode is controlled by the following properties:

  • VertAlign (AlignType enum)

    This controls the vertical alignment of the control, it can be either Left, Center, or Right.

  • BarCode (string)

    This is the text to be displayed as a barcode.

  • BarCodeHeight (int)

    This is the height in pixels of the barcode.

  • LeftMargin (int)

    The size of the left margin.

  • TopMargin (int)

    The size of the top margin.

  • HeaderText (string)

    The text to be displayed in the header.

  • ShowHeader (bool)

    Shows the header text specified by the HeaderText property.

  • ShowFooter (bool)

    Shows the footer, which is the text representation of the barcode.

  • Weight (BarCodeWeight enum)

    This is the weight of the barcode, it will affect how wide it is displayed. The values are Small, Medium and Large.

  • HeaderFont (Font)

    The font of the header text.

  • FooterFont (Font)

    The font of the footer text.

Using the code

The code is very simple to use, just plop the control onto a form and you are ready to start customizing it via the Properties window or through your code.

In addition to the properties, there are also two public functions of interest:

  • public void Print()

    This function will display a print dialog and then print the contents of the control to the selected printer.

  • public void SaveImage(string filename)

    This function will save the contents of the control to a bitmap image specified by filename.

Conclusion

There are other types of barcodes out there, code 39 seems to be a pretty popular one. I hope that this control fits your needs and that you find it easy to use. If there are any features that you feel it is missing, I would like to hear about it and maybe I will update the control. I think that you will find the code very simple and easy to modify if you want to use it as a base for your own control.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
United States United States
I live in the Nothern Virginia/Washington D.C. area. I have been working in the software industry since 1995. My skills are primarily in C++ and Java on Windows and Unix platforms but I also work with C# and some other programming languages (even PL/I and COBOL when necessary!).

Check out my software web site: http://www.dreamsyssoft.com

Comments and Discussions

 
GeneralThanks! Pin
Gord4431-Oct-07 11:16
Gord4431-Oct-07 11:16 
GeneralASP.NET Pin
a3r0sol15-Oct-07 23:08
a3r0sol15-Oct-07 23:08 
GeneralExtended Pin
MH25383-Jun-07 1:24
MH25383-Jun-07 1:24 
GeneralThank you! Pin
hendry_johan13-May-07 8:37
hendry_johan13-May-07 8:37 
GeneralLooking for code 128 sample also Pin
Jadhaw14-Mar-07 15:44
Jadhaw14-Mar-07 15:44 
GeneralRe: Looking for code 128 sample also Pin
BruceN16-Jul-07 17:27
BruceN16-Jul-07 17:27 
GeneralCode-128 Pin
schunnu14-Mar-07 7:09
schunnu14-Mar-07 7:09 
GeneralRe: Code-128 Pin
BruceN16-Jul-07 17:31
BruceN16-Jul-07 17:31 
Look at the reply above
GeneralThanks Pin
some_really_long_unique_name26-Feb-07 20:59
some_really_long_unique_name26-Feb-07 20:59 
GeneralMany Thanks Pin
cs25617-Aug-06 5:00
cs25617-Aug-06 5:00 
GeneralReg: Reading BarCode Value Pin
Kumar Murugesan26-Jul-06 4:36
Kumar Murugesan26-Jul-06 4:36 
GeneralExtra Line of Text and Barcode Pin
AesopTurtle11-May-06 18:55
AesopTurtle11-May-06 18:55 
GeneralRe: Extra Line of Text and Barcode Pin
enlikil26-Oct-06 6:19
enlikil26-Oct-06 6:19 
General2D barcode Pin
mohammed barqawi30-Nov-05 21:01
mohammed barqawi30-Nov-05 21:01 
GeneralLeading and Trailing character "*" Pin
vkv86-Oct-05 10:27
vkv86-Oct-05 10:27 
GeneralMany thanks Pin
WillemM22-Sep-05 21:32
WillemM22-Sep-05 21:32 
QuestionBarcode generation for web application Pin
Carlos Eugênio X. Torres30-Aug-05 6:42
Carlos Eugênio X. Torres30-Aug-05 6:42 
AnswerMessage Closed Pin
9-Nov-06 7:56
lesnikowski9-Nov-06 7:56 
Questionhow to import Pin
Dregor27-Aug-05 12:16
Dregor27-Aug-05 12:16 
AnswerRe: how to import Pin
AesopTurtle11-May-06 16:19
AesopTurtle11-May-06 16:19 
GeneralNice Clean Solution Pin
evileddie8-Aug-05 13:09
evileddie8-Aug-05 13:09 
GeneralI2OF5 Pin
rlgperez28-Jul-05 14:15
rlgperez28-Jul-05 14:15 
GeneralMessage Closed Pin
9-Nov-06 7:58
lesnikowski9-Nov-06 7:58 
GeneralDear rocky_pulley Pin
gudman28-May-05 1:21
gudman28-May-05 1:21 
GeneralRe: Dear rocky_pulley Pin
rocky_pulley28-May-05 4:34
rocky_pulley28-May-05 4:34 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.