Click here to Skip to main content
6,595,444 members and growing! (17,085 online)
Email Password   helpLost your password?
Desktop Development » Miscellaneous » Miscellaneous Controls     Intermediate

Barcode .NET Control

By rocky_pulley

A Code 39 barcode control with printing and saving support.
C#, VC7, VC7.1, VC8.0.NET 1.1, Win2K, WinXP, Win2003, Vista, GDI+, VS.NET2003, Dev
Posted:10 May 2005
Views:97,359
Bookmarked:129 times
Announcements
Loading...
 
Search    
Advanced Search
Add to IE Search
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
34 votes for this article.
Popularity: 6.81 Rating: 4.44 out of 5
2 votes, 5.9%
1

2

3
6 votes, 17.6%
4
26 votes, 76.5%
5

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

About the Author

rocky_pulley


Member
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


Occupation: Web Developer
Location: United States United States

Other popular Miscellaneous articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 25 of 36 (Total in Forum: 36) (Refresh)FirstPrevNext
GeneralNeed Help PinmemberManoj K Dubey3:17 21 Apr '09  
QuestionThanks for your great code PinmemberManoj K Dubey22:30 13 Apr '09  
GeneralSimple and Nice Pinmembergistexan13:12 2 Apr '09  
GeneralNice job Pinmemberdlj780:10 7 Jan '09  
QuestionLocalizations PinmemberWaleedH0:48 29 Apr '08  
GeneralBarcode for .NET/ASP.NET from BarcodeLib.com [modified] Pinmemberbarcodelibcom22:41 5 Mar '08  
GeneralThanks Pinmemberfatih isikhan7:09 17 Jan '08  
GeneralWhy can't I add * [modified] PinmemberThorgalRose22:09 28 Nov '07  
Generalprint from Internet explorer PinmemberSu_shamim22:17 24 Nov '07  
GeneralHow to print 2D PDF 417 Bar code using c# Pinmemberchristokavi1:21 1 Nov '07  
GeneralThanks! PinmemberGord4412:16 31 Oct '07  
GeneralASP.NET Pinmembera3r0sol0:08 16 Oct '07  
GeneralExtended PinmemberMH25382:24 3 Jun '07  
GeneralThank you! Pinmemberhendry_johan9:37 13 May '07  
GeneralLooking for code 128 sample also PinmemberJadhaw16:44 14 Mar '07  
GeneralRe: Looking for code 128 sample also PinmemberBruceN18:27 16 Jul '07  
GeneralCode-128 Pinmemberschunnu8:09 14 Mar '07  
GeneralRe: Code-128 PinmemberBruceN18:31 16 Jul '07  
GeneralThanks Pinmembersome_really_long_unique_name21:59 26 Feb '07  
GeneralMany Thanks Pinmembercs2566:00 17 Aug '06  
GeneralReg: Reading BarCode Value PinmemberKumar Murugesan5:36 26 Jul '06  
GeneralExtra Line of Text and Barcode PinmemberKiTsuNeKo19:55 11 May '06  
GeneralRe: Extra Line of Text and Barcode Pinmemberenlikil7:19 26 Oct '06  
General2D barcode Pinmembermohammed barqawi22:01 30 Nov '05  
GeneralLeading and Trailing character "*" PinmemberVKV811:27 6 Oct '05  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 10 May 2005
Editor: Smitha Vijayan
Copyright 2005 by rocky_pulley
Everything else Copyright © CodeProject, 1999-2009
Web11 | Advertise on the Code Project