简介
本文及其代码为那些需要在应用程序中放置条形码的开发人员提供了一种方法。 它允许生成条形码图像,而无须使用“条码字体”。This need arose out of the necessity for one of my own projects to use barcodes and the lack of free libraries on the web to do the job.
在此类库中给出了编码的思路。 首先将数据转换为 1 和 0 的等宽条形和空白描述 系列。 然后将二进制信息 字符串传递 到一个绘图函数,绘图函数将它转换为所需的条形码图像。这种方法可以作为一个通用的绘图方法,适用于所有的图像编码 。
代码使用
此类库包含一个名为 Barcode 的类(BarcodeLib 命名空间)。Barcode 类有三个构造函数:
Barcode();
Barcode(string
data);
Barcode(string
data, BarcodeLib.TYPE iType);
如果你决定使用参数创建此类的实例,参数如下:
string data : 准备编码为条形码的数据
BarcodeLib.TYPE iType: 数据的编码方法(编码类型)
如果在创建类的实例 时,没有指定编码数据和类型(参数),以后你可以通过设置适当的属性值来指定它们(但是需要在编码之前指定)。
BarCodeLib.Barcode b = new
BarCodeLib.Barcode("038000356216"
, BarCodeLib.TYPE.UPCA);
要得到数据编码后生成的条形码图像,你必须调用下列的 Encode 函数之一:
public
Image Encode(TYPE iType, string
StringToEncode, double
percent)
public
Image Encode(TYPE iType, double
percent)
public
Image Encode(TYPE iType, string
StringToEncode, int
Width, int
Height)
public
Image Encode(TYPE iType, int
Width, int
Height)
public
Image Encode(TYPE iType, string
StringToEncode, Color DrawColor, Color BackColor, double
percent)
public
Image Encode(TYPE iType, string
StringToEncode, Color DrawColor, Color BackColor, int
Width, int
Height)
public
Image Encode(TYPE iType, string
StringToEncode, Color DrawColor, Color BackColor)
public
Image Encode(TYPE iType, Color DrawColor, Color BackColor)
public
Image Encode(TYPE iType, string
StringToEncode)
public
Image Encode(TYPE iType)public
Image Encode()
Image 类型的返回结果包含了图像格式的条形码。
在类中还包含更多的函数,你可以在编码后就把图像保存到文件中。
public
void
SaveImage(string
Filename, SaveTypes FileType)
可以通过指定一个保存图像的位置全路径(包含文件名)字符串来使用此函数。第二个参数是一个枚举 (BarcodeLib.SaveTypes ) ,描述了所支持的多种文件保存格式类型 (JPG, BMP, PNG, GIF, TIFF) 。
关注点
在编写这个类库时,为我提供了这样一个机会:深入了解 条 形码编码方法的工作机制,以及编码方法之间的差异(While writting this library it offered me the chance to become intimately familiar with how barcode symbologies work and how the symbologies differ from one another)。
版本更新记录
2007.10.10 - 初版 (存在 bug,并需要修改或实现某些编码法) (译者注:请读者关注作者的更新)
2007.10.16 - Encode_Code39() 函数修复了一个 bug(未包含 interchar spaces);更新了 Generate_Image(Color, Color) 函数,并把 using SetPixel 代码段替换 为下列代码 :
using
(Graphics g = Graphics.FromImage(b))
{
g.DrawLine(new
Pen(c), x, 0, x, b.Height);
g.DrawLine(new
Pen(c), x + 1, 0, x + 1, b.Height);
}