我需要使用 GDI+ 打印和交错 5 个条形码中的 2 个(18 位无校验和)。我已经使用网上找到的一些代码对代码 128 条码进行了此操作。不幸的是,条形码扫描仪无法读取代码 128,因此我不得不求助于 ITF 条形码。
谢谢,肯
class I2of5
{
private static string[] patterns =
{
"NNWWN",//0
"WNNNW",//1
"NWNNW",//2
"WWNNN",//3
"NNWNW",//4
"WNWNN",//5
"NWWNN",//6
"NNNWW",//7
"WNNWN",//8
"NWNWN"//9
};
public I2of5()
{
}
public static Image MakeBarcodeImage(string barcodeNumber)
{
int width = 420;
int height = 60;
string barcodeString = "NnNn";//start pattern
int barcodeLength = barcodeNumber.Length;
for(int i =0 ; i < barcodeLength; i++)
{
int firstNumber = int.Parse(barcodeNumber[i].ToString());
int secondNumber = int.Parse(barcodeNumber[i + 1].ToString());
string firstPattern = patterns[firstNumber].ToUpper();
string secondPattern = patterns[secondNumber].ToLower();
barcodeString += String.Format("{0}{1}{2}{3}{4}{5}{6}{7}{8}{9}", firstPattern[0], secondPattern[0],
firstPattern[1] , secondPattern[1] ,
firstPattern[2] , secondPattern[2] ,
firstPattern[3] , secondPattern[3] ,
firstPattern[4] , secondPattern[4]);
i++;
}
barcodeString +="WnN";//stop pattern
Image barcodeImage = new System.Drawing.Bitmap(width, height);
using (Graphics gr = Graphics.FromImage(barcodeImage))
{
// set to white so we don't have to fill the spaces with white
gr.FillRectangle(Brushes.White, 0, 0, width, height);
int cursor = 0;
for (int codeidx = 0; codeidx < barcodeString.Length; codeidx++)
{
char code = barcodeString[codeidx];
int BarWeight = 1;
int barwidth = ((code == 'N') || (code == 'n'))?2 * BarWeight: 6 * BarWeight;
if((code == 'N') || (code == 'W'))
{
gr.FillRectangle(System.Drawing.Brushes.Black, cursor, 0, barwidth , height);
}
// note that we never need to draw the space, since we
// initialized the graphics to all white
// advance cursor beyond this pair
cursor += barwidth;
}
}
return barcodeImage;
}
我不了解 GDI+,但您可以使用 iTextSharp(开源)来做到这一点。这里有一个例子:http: //itextsharp.sourceforge.net/examples/Chap0907.cs
并在此处进行解释:http: //itextsharp.sourceforge.net/tutorial/ch09.html#barcode