Drawing POSTNET Barcodes with C#
上篇:利用PEAR 和 PHP给你的网络应用添加条形码
下篇:Code 128 Barcode Font Advantage Package 的常见问题

Introduction
This article investigates the POSTNET barcode system, and demonstrates some basic functionality to create POSTNET barcodes using C# 2005 and GDI+.
Background
Many employers these days are fairly frugal, so having them purchase something as simple as postal barcode fonts sometimes can prove to be extremely difficult. Being in this position a time or two, I decided to investigate the postal barcode system and create a simple library that could draw the POSTNET barcodes when given the zip code.
POSTNET Barcode Overview
POSTNET barcodes can be composed of several pieces of information: frame bars, zip code, +4, delivery point code, and correction character (see image below).

Frame bars signify the beginning and the end of the barcode. The frame bars are mandatory, and are composed of a single Full Bar. The zip code is also mandatory, and is represented by 25 bars. The +4 is composed of 20 bars and is optional. The delivery point code is represented by 10 bars, is optional, and is normally the last two digits of the street address, post office box, rural route number, or highway contract route number. The correction character is mandatory, and is basically a check sum (I use the terms correction character and check sum interchangeably in this article).
![]() |
Each digit is represented by a distinct pattern composed of five bars (see image on left for patterns). Basically, there are two distinct types of bars: the Full Bar and the Half Bar. A Full Bar must be between a minimum of 0.115" and a maximum of 0.135" tall. A Half Bar must be between a minimum of 0.04" and a maximum of 0.06" tall. Both the Full Bar and the Half Bar must be between 0.015" and 0.025" wide. In addition to the height and width specifications, there is spacing requirement between bars; it must be between 0.012" and 0.040". Specifications for POSTNET Barcode
(For a complete reference, please refer to the Postal Service POSTNET barcode specifications available here, or do a search on POSTNET barcode specifications.) |
Correction Character
The correction character is a simple check sum which is calculated by adding each of the numbers in the zip code together, takes the modulus of the summed numbers by 10, and then subtracts this result from 10.
Example: Zip code + 4 is 12345-6789 with a delivery point of 01.
1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 0 + 1 = 46
46 % 10 = 6
10 - 6 = 4
4 % 10 = 4
So the correction character is 4; the last modulus is for the special case when the summed zip code is a multiple of 10.
Code
The Postnet class is used to render the barcode using a handle to a Graphics object or create a Bitmap class. The only required data is the zip code; the +4 digits and the delivery point are optional. The correction character will be calculated by the Postnet class.
Using the Code
The Postnet class will handle all aspects of rendering the barcode. Simply create an instance of the Postnet class, passing the zip code to the constructor as a parameter. Then, call the DrawPOSTNET function passing the Graphics object and the starting point as parameters. Once it returns from processing, handle any other drawing tasks, and then clean up by calling the Dispose function of the Graphics object.
// Create an instance of the Postnet class and send it
// the zip code, +4, and delivery point. The Postnet class will
// calculate the correction character.
Postnet ps = new Postnet( "12345", "6789", "01" );
// Create a Graphics object handle from a picture box.
System.Drawing.Graphics g = this.pictureBox1.CreateGraphics( );
// Send a handle to the Graphics object and the starting
// point to begin drawing the barcode.
ps.DrawPOSTNET( g, new System.Drawing.Point( 0, 0 ) );
// Handle any other drawing necessary.
// Clean up.
g.Dispose( );
The second functionality that the Postnet class provides is to create the barcode in the form a bitmap. To create a bitmap, create an instance of the Postnet class passing the zip code to the constructor. Then, call the CreateBitmap function passing the width and height for the bitmap that will be created.
// Create an instance of the Postnet class and send it
// the zip code, +4, and delivery point. The Postnet class will
// calculate the correction character.
Postnet ps = new Postnet( "12345", "6789", "01" );
// Call the CreateBitmap function passing in the width and height for the
// newly created bitmap.
pictureBox1.Image = ps.CreateBitmap( this.pictureBox1.Width,
this.pictureBox1.Height );
How the code works
The Postnet class is relatively simple. It provides several constructors for various means of creation; several properties/attributes to set and get class data; the two public functions discussed above, and a few private functions which handle some of the grunt work.
When an instance of the class is created, it initializes the bar specifications by calling the InitBarSpecification function. This function sets several pieces of information, which controls the various aspects of the bars, and initializes the DRAWING_INFO structure (see code listed below).
private void InitBarSpecifications( )
{
// Set the Bar specifications with the default values.
di = new DRAWING_INFO( );
_fBarWidth = .022f;
_fTallBarHeight = 0.125f;
_fShortBarHeight = 0.05f;
_fBarSpacing = 0.02f;
}
The DRAWING_INFO contains the instance of the brush used to draw the bars, the current x location, and the current y location.
相关文章
·利用PEAR 和 PHP给你的网络应用添加条形码
·Code 128 Barcode Font Advantage Package 的常见问题
·从图片中取出barocode
·Barbecue - Java Barcode
·Code 128 Barcode Font Advantage Package 中如何手动加入起始符,结束符,校验符
·使用 Java 读取条形码和维护库存数据库
·中国最全的barcode条码开发资料!
·Aspose.BarCode for Java 1.0.0.0发布啦
