Click here to Skip to main content
15,881,866 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 419.4K   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

 
Questionnice code Pin
hisham elmsry9-Mar-23 4:42
hisham elmsry9-Mar-23 4:42 
PraiseLOVE IT Pin
Joseph Guenther23-Oct-21 7:26
Joseph Guenther23-Oct-21 7:26 
GeneralBarcode Pin
dappertje5-Dec-18 10:55
dappertje5-Dec-18 10:55 
QuestionOFFTOPIC Pin
E P Sharp16-May-15 3:48
E P Sharp16-May-15 3:48 
QuestionLicense? Pin
ByteBean4-Mar-15 4:56
ByteBean4-Mar-15 4:56 
QuestionI have a problem after creating barcode using this demo project Pin
Member 1141298530-Jan-15 6:45
Member 1141298530-Jan-15 6:45 
Questionbar code Pin
Member 1033549814-Oct-13 6:31
Member 1033549814-Oct-13 6:31 
QuestionFixed Same Width for all data Pin
MichealRay1-Sep-13 23:02
MichealRay1-Sep-13 23:02 
GeneralMy vote of 4 Pin
Member 873007328-Aug-13 15:49
Member 873007328-Aug-13 15:49 
QuestionWhat is the license for this project? Pin
Pimorn Senakat27-Jun-13 22:47
Pimorn Senakat27-Jun-13 22:47 
QuestionCreate Barcode&QRcode Pin
Member 917112825-Nov-12 21:24
Member 917112825-Nov-12 21:24 
QuestionWhat about Reports??? Pin
Member 95659483-Nov-12 7:33
Member 95659483-Nov-12 7:33 
GeneralMy vote of 5 Pin
Pasan Eeriyagama31-Oct-12 17:37
Pasan Eeriyagama31-Oct-12 17:37 
QuestionDid you create your own customized library for the barcode?How to find API's to generate barcodes in C#? Pin
Akshay_8816-Jul-12 23:12
Akshay_8816-Jul-12 23:12 
QuestionLegendary Pin
ak1612-Jul-12 7:49
ak1612-Jul-12 7:49 
GeneralMy vote of 5 Pin
kannan.dk28-May-12 23:43
kannan.dk28-May-12 23:43 
GeneralMy vote of 5 Pin
Mário Rui Cláudio22-May-12 4:39
Mário Rui Cláudio22-May-12 4:39 
SuggestionThanks So much!! Pin
blue_sky250319-Apr-12 22:05
blue_sky250319-Apr-12 22:05 
GeneralMy vote of 5 Pin
Manoj Kumar Choubey23-Feb-12 21:06
professionalManoj Kumar Choubey23-Feb-12 21:06 
QuestionTHE BEST !!! Pin
nereocorp6-Feb-12 12:42
nereocorp6-Feb-12 12:42 
QuestionPrint function is not working for me, any help ? Pin
kaypees8721-Jun-11 22:25
kaypees8721-Jun-11 22:25 
GeneralMuchas Gracias!!! Pin
Member 81686922-Apr-11 20:29
Member 81686922-Apr-11 20:29 
GeneralReally Great :) Pin
Member 38127938-Feb-11 9:00
Member 38127938-Feb-11 9:00 
GeneralMy vote of 5 Pin
Theingi Win21-Dec-10 13:08
Theingi Win21-Dec-10 13:08 
Questionbarcode reader doesnt read this barcode Pin
karadogan10-Dec-10 21:33
karadogan10-Dec-10 21:33 

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.