參考來源:
http://www.techrepublic.com/blog/howdoi/how-do-i-generate-barcodes-using-c/173
1.建立將文字轉條碼的類別
public static Bitmap CreateBarcode(string sData)
{
//create Bitmap物件
Bitmap barcode = new Bitmap(1, 1);
//設定條碼字型
Font threeOfNine = new Font("Free 3 of 9", 60, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point);
//create圖形物件、載入Barcode字型
Graphics graphics = Graphics.FromImage(barcode);
//設定轉出條碼後的寬度、高度及字型
SizeF datasize = graphics.MeasureString(sData, threeOfNine);
//now we base the bitmap's size off of this data
barcode = new Bitmap(barcode, datasize.ToSize());
//依據更新後的bitmap刷新圖型物件
graphics = Graphics.FromImage(barcode);
//設定圖型物件背景顏色
graphics.Clear(Color.White);
//設定圖型物件的關聯文字的呈現模式為 SingleBitPerPixel
//graphics.TextRenderingHint = TextRenderingHint.SingleBitPerPixel;
graphics.TextRenderingHint = System.Drawing.Text.TextRenderingHint.SingleBitPerPixel;
//將字串資料放入圖型物件
graphics.DrawString(sData, threeOfNine, new SolidBrush(Color.Black), 0, 0);
//圖型物件刷新
graphics.Flush();
//釋放物件
threeOfNine.Dispose();
graphics.Dispose();
//回傳
return barcode;
}
2.畫面放一個Button、TextBox,Button事件裡面
Bitmap barcode = HClass1.CreateBarcode("*" + TextBox1.Text + "*");
barcode.Save(@"c:\barcode.gif", System.Drawing.Imaging.ImageFormat.Gif);
沒有留言:
張貼留言