1.前言:
在生產系統中,需要用到大量的條碼標籤列印,本範例利用GDI+繪圖的方式,產生條碼的圖檔,可供列印條碼標籤。

2.說明:
條碼的基本說明可參考
http://en.wikipedia.org/wiki/Barcode
本範例介紹Code39條碼,其相關資訊請參考
http://en.wikipedia.org/wiki/Code_39

建立CodeSystem類別

public class CodeSystem
{
	public static Hashtable Code39()
	{
		Hashtable code = new Hashtable();

		code.Add("0", "bwbWBwBwb");
		code.Add("1", "BwbWbwbwB");
		code.Add("2", "bwBWbwbwB");
		code.Add("3", "BwBWbwbwb");
		code.Add("4", "bwbWBwbwB");
		code.Add("5", "BwbWBwbwb");
		code.Add("6", "bwBWBwbwb");
		code.Add("7", "bwbWbwBwB");
		code.Add("8", "BwbWbwBwb");
		code.Add("9", "bwBWbwBwb");
		code.Add("A", "BwbwbWbwB");
		code.Add("B", "bwBwbWbwB");
		code.Add("C", "BwBwbWbwb");
		code.Add("D", "bwbwBWbwB");
		code.Add("E", "BwbwBWbwb");
		code.Add("F", "bwBwBWbwb");
		code.Add("G", "bwbwbWBwB");
		code.Add("H", "BwbwbWBwb");
		code.Add("I", "bwBwbWBwb");
		code.Add("J", "bwbwBWBwb");
		code.Add("K", "BwbwbwbWB");
		code.Add("L", "bwBwbwbWB");
		code.Add("M", "BwBwbwbWb");
		code.Add("N", "bwbwBwbWB");
		code.Add("O", "BwbwBwbWb");
		code.Add("P", "bwBwBwbWb");
		code.Add("Q", "bwbwbwBWB");
		code.Add("R", "BwbwbwBWb");
		code.Add("S", "bwBwbwBWb");
		code.Add("T", "bwbwBwBWb");
		code.Add("U", "BWbwbwbwB");
		code.Add("V", "bWBwbwbwB");
		code.Add("W", "BWBwbwbwb");
		code.Add("X", "bWbwBwbwB");
		code.Add("Y", "BWbwBwbwb");
		code.Add("Z", "bWBwBwbwb");
		code.Add("-", "bWbwbwBwB");
		code.Add(".", "BWbwbwBwb");
		code.Add(" ", "bWBwbwBwb");
		code.Add("$", "bWbWbWbwb");
		code.Add("/", "bWbWbwbWb");
		code.Add("+", "bWbwbWbWb");
		code.Add("%", "bwbWbWbWb");
		code.Add("*", "bWbwBwBwb");

		return code;
	}
}

建立Code39影像檔方法

private Bitmap Code39(string strInput)
{
	//Code 39 specification
	int narrowWidth = 1;
	int wideWidth = 2;
	int B = wideWidth;//Wide-Black width
	int b = narrowWidth;//Narrow-Black width
	int W = wideWidth;//Wide-White width
	int w = narrowWidth;//Narrow-White width            
	int padding = 15;
	int startPoint = padding;//繪製線條起點
	int strLength = strInput.Length;
	FontFamily fontFamily = new FontFamily("Arial");
	Font myFont = new Font(fontFamily, 9, FontStyle.Regular, GraphicsUnit.Point);
	int barcodeHeight = 60;
	int barcodeWidth = (wideWidth * 3 + narrowWidth * 7) * (strLength + 2); ;

	//定義影像檔
	/*寬度設定定義: 
	  以A為例,code為BwbwbWbwB加上結尾w共十個字元,其中寬線共三個,窄線共7個;
	  字串長度加上前後的*;
	  前後留白的寬度x2
	*/
	int imgWidth = ((wideWidth * 3 + narrowWidth * 7) * (strLength + 2)) + padding * 2;
	int imgHeight = barcodeHeight + padding * 2;
	pictureBox1.Width = imgWidth;
	pictureBox1.Height = imgHeight;
	Bitmap bitmap = new Bitmap(imgWidth, imgHeight);
	//在image上繪圖
	Graphics g = Graphics.FromImage(bitmap);
	g.PageUnit = GraphicsUnit.Pixel;
	//繪圖品質設定
	g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias;
	g.PixelOffsetMode = System.Drawing.Drawing2D.PixelOffsetMode.HighQuality;
	g.CompositingQuality = CompositingQuality.HighQuality;
	g.SmoothingMode = SmoothingMode.AntiAlias;
	g.InterpolationMode = InterpolationMode.High;
	g.PixelOffsetMode = PixelOffsetMode.HighQuality;
	//先將背景色設定為白色
	g.FillRectangle(new SolidBrush(Color.White), 0, 0, bitmap.Width, bitmap.Height);

	string codeString = "";
	Hashtable getCode = CodeSystem.Code39();

	for (int i = 0; i < strInput.Length; i++)
	{
		string character = strInput[i].ToString().ToUpper();
		if (getCode[strInput[i].ToString().ToUpper()] != null && getCode[strInput[i].ToString().ToUpper()].ToString() != "*")
		{
			codeString += getCode[strInput[i].ToString().ToUpper()].ToString() + "w";
		}
		else
		{
			g.DrawString("Invalid\nCharacter", myFont, Brushes.Black, 0, (barcodeHeight + padding * 2) / 3);
			return bitmap;
		}
	}
	//前後用*包起字串,例如*Code39*
	codeString = getCode["*"].ToString() + "w" + codeString + getCode["*"].ToString();
	Console.WriteLine(codeString);
	int shiftPoint = 0;
	for (int i = 0; i < codeString.Length; i++)
	{
		string s = codeString[i].ToString();

		switch (s)
		{
			case "B":
				shiftPoint = B;
				break;
			case "b":
				shiftPoint = b;
				break;
			case "W":
				shiftPoint = W;
				break;
			case "w":
				shiftPoint = w;
				break;
		}

		g.FillRectangle(i % 2 == 0 ? new SolidBrush(Color.Black) : new SolidBrush(Color.White), startPoint, padding, shiftPoint, barcodeHeight);
		startPoint += shiftPoint;
	}

	//Draw footer string
	string footerString = "*" + strInput + "*";
	//g.DrawString(footerString, myFont, Brushes.Black, (float)(padding / 2f + imgWidth / 2f - (footerString.Length * myFont.Size / 2f)), (float)(barcodeHeight + padding + 2f));
	TextRenderer.DrawText(g, footerString, myFont, new Point((int)(padding / 2f + imgWidth / 2f - (footerString.Length * myFont.Size / 2f)), (int)(barcodeHeight + padding + 2f)), SystemColors.ControlText);

	return bitmap;
}

3.應用:

//定義影像檔
Bitmap bitmap = Code39("PIXNET");
//顯示影像
pictureBox1.Image = bitmap;

圖例:

Code39  

arrow
arrow
    文章標籤
    C# Code39 GDI+ Barcode
    全站熱搜
    創作者介紹
    創作者 西夏普 的頭像
    西夏普

    西夏普的部落格

    西夏普 發表在 痞客邦 留言(0) 人氣()