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 =
{
"000110100",
"100100001",
"001100001",
"101100000",
"000110001",
"100110000",
"001110000",
"000100101",
"100100100",
"001100100",
"100001001",
"001001001",
"101001000",
"000011001",
"100011000",
"001011000",
"000001101",
"100001100",
"001001100",
"000011100",
"100000011",
"001000011",
"101000010",
"000010011",
"100010010",
"001010010",
"000000111",
"100000110",
"001000110",
"000010110",
"110000001",
"011000001",
"111000000",
"010010001",
"110010000",
"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.