Click here to Skip to main content
12,395,593 members (59,358 online)
Click here to Skip to main content
Add your own
alternative version

Stats

298.4K views
37.4K downloads
191 bookmarked
Posted

Barcode .NET Control

, 10 May 2005
Rate this:
Please Sign up or sign in to vote.
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.

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

Share

About the Author

rocky_pulley
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


You may also be interested in...

Comments and Discussions

 
QuestionOFFTOPIC Pin
E P Sharp16-May-15 3:48
memberE P Sharp16-May-15 3:48 
QuestionLicense? Pin
ByteBean4-Mar-15 4:56
memberByteBean4-Mar-15 4:56 
QuestionI have a problem after creating barcode using this demo project Pin
Member 1141298530-Jan-15 6:45
memberMember 1141298530-Jan-15 6:45 
Questionbarcode generator for .net Pin
Member 109392318-Sep-14 21:33
memberMember 109392318-Sep-14 21:33 
Questionbar code Pin
Member 1033549814-Oct-13 6:31
memberMember 1033549814-Oct-13 6:31 
QuestionFixed Same Width for all data Pin
MichealRay1-Sep-13 23:02
memberMichealRay1-Sep-13 23:02 
GeneralMy vote of 4 Pin
Member 873007328-Aug-13 15:49
memberMember 873007328-Aug-13 15:49 
QuestionWhat is the license for this project? Pin
Pimorn Senakat27-Jun-13 22:47
memberPimorn Senakat27-Jun-13 22:47 
QuestionCreate Barcode&QRcode Pin
Member 917112825-Nov-12 21:24
memberMember 917112825-Nov-12 21:24 
QuestionWhat about Reports??? Pin
Member 95659483-Nov-12 7:33
memberMember 95659483-Nov-12 7:33 
GeneralMy vote of 5 Pin
Pasan Indeewara31-Oct-12 17:37
memberPasan Indeewara31-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
memberAkshay_8816-Jul-12 23:12 
QuestionLegendary Pin
ak1612-Jul-12 7:49
memberak1612-Jul-12 7:49 
GeneralMy vote of 5 Pin
kannan.dk28-May-12 23:43
memberkannan.dk28-May-12 23:43 
GeneralMy vote of 5 Pin
Mario Rui Claudio22-May-12 4:39
memberMario Rui Claudio22-May-12 4:39 
SuggestionThanks So much!! Pin
blue_sky250319-Apr-12 22:05
memberblue_sky250319-Apr-12 22:05 
Question.NET EAN128 library Pin
james99115-Mar-12 23:02
memberjames99115-Mar-12 23:02 
GeneralMy vote of 5 Pin
manoj kumar choubey23-Feb-12 21:06
membermanoj kumar choubey23-Feb-12 21:06 
QuestionTHE BEST !!! Pin
nereocorp6-Feb-12 12:42
membernereocorp6-Feb-12 12:42 
GeneralMy vote of 5 Pin
Zacbr8-Jan-12 3:10
memberZacbr8-Jan-12 3:10 
QuestionPrint function is not working for me, any help ? Pin
kaypees8721-Jun-11 22:25
memberkaypees8721-Jun-11 22:25 
GeneralMuchas Gracias!!! Pin
Member 81686922-Apr-11 20:29
memberMember 81686922-Apr-11 20:29 
GeneralReally Great :) Pin
Member 38127938-Feb-11 9:00
memberMember 38127938-Feb-11 9:00 
GeneralMy vote of 5 Pin
theingiwin21-Dec-10 13:08
membertheingiwin21-Dec-10 13:08 
Questionbarcode reader doesnt read this barcode Pin
karadogan10-Dec-10 21:33
memberkaradogan10-Dec-10 21:33 
QuestionHow to read this barcode?? [modified] Pin
karadogan6-Dec-10 2:48
memberkaradogan6-Dec-10 2:48 
GeneralThanks Pin
Member 43026751-Nov-10 2:08
memberMember 43026751-Nov-10 2:08 
GeneralMy vote of 5 Pin
Member 298078924-Oct-10 10:00
memberMember 298078924-Oct-10 10:00 
QuestionHow we can use this in my VB 6 application Pin
dheeraj.mittal3-Aug-10 21:45
memberdheeraj.mittal3-Aug-10 21:45 
GeneralNice article Pin
kyle198318-Mar-10 19:25
memberkyle198318-Mar-10 19:25 
GeneralNeed Help Pin
Manoj K Dubey21-Apr-09 2:17
memberManoj K Dubey21-Apr-09 2:17 
QuestionThanks for your great code Pin
Manoj K Dubey13-Apr-09 21:30
memberManoj K Dubey13-Apr-09 21:30 
GeneralSimple and Nice Pin
gistexan2-Apr-09 12:12
membergistexan2-Apr-09 12:12 
GeneralNice job Pin
dlj786-Jan-09 23:10
memberdlj786-Jan-09 23:10 
QuestionLocalizations Pin
WaleedH28-Apr-08 23:48
memberWaleedH28-Apr-08 23:48 
GeneralBarcode for .NET/ASP.NET from BarcodeLib.com [modified] Pin
barcodelibcom5-Mar-08 21:41
memberbarcodelibcom5-Mar-08 21:41 
GeneralThanks Pin
fatih isikhan17-Jan-08 6:09
memberfatih isikhan17-Jan-08 6:09 
GeneralWhy can't I add * [modified] Pin
ThorgalRose28-Nov-07 21:09
memberThorgalRose28-Nov-07 21:09 
Generalprint from Internet explorer Pin
Su_shamim24-Nov-07 21:17
memberSu_shamim24-Nov-07 21:17 
QuestionHow to print 2D PDF 417 Bar code using c# Pin
christokavi1-Nov-07 0:21
memberchristokavi1-Nov-07 0:21 
GeneralThanks! Pin
Gord4431-Oct-07 11:16
memberGord4431-Oct-07 11:16 
GeneralASP.NET Pin
a3r0sol15-Oct-07 23:08
membera3r0sol15-Oct-07 23:08 
GeneralExtended Pin
MH25383-Jun-07 1:24
memberMH25383-Jun-07 1:24 
GeneralThank you! Pin
hendry_johan13-May-07 8:37
memberhendry_johan13-May-07 8:37 
GeneralLooking for code 128 sample also Pin
Jadhaw14-Mar-07 15:44
memberJadhaw14-Mar-07 15:44 
GeneralRe: Looking for code 128 sample also Pin
BruceN16-Jul-07 17:27
memberBruceN16-Jul-07 17:27 
GeneralCode-128 Pin
schunnu14-Mar-07 7:09
memberschunnu14-Mar-07 7:09 
GeneralRe: Code-128 Pin
BruceN16-Jul-07 17:31
memberBruceN16-Jul-07 17:31 
GeneralThanks Pin
some_really_long_unique_name26-Feb-07 20:59
membersome_really_long_unique_name26-Feb-07 20:59 
GeneralMany Thanks Pin
cs25617-Aug-06 5:00
membercs25617-Aug-06 5:00 

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.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.160721.1 | Last Updated 10 May 2005
Article Copyright 2005 by rocky_pulley
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid