65.9K
CodeProject is changing. Read more.
Home

Barcode .NET Control

starIconstarIconstarIconstarIcon
emptyStarIcon
starIcon

4.82/5 (62 votes)

May 10, 2005

3 min read

viewsIcon

427578

downloadIcon

47062

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:

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.