最近我項目需要生成條碼,那我就找了一下相關資料發現BarcodeLib來生成條碼,不需要什么字體庫,引用就可,寫下來備我以后資料查詢
using BarcodeLib;
using System.IO;
static System.Drawing.Image GetBarcode(int height, int width, BarcodeLib.TYPE type,string code,out System.Drawing.Image image)
{
image=null;
BarcodeLib.Barcode b = new BarcodeLib.Barcode();
b.BackColor = System.Drawing.Color.White;//圖片背景顏色
b.ForeColor = System.Drawing.Color.Black;//條碼顏色
b.IncludeLabel = true;
b.Alignment = BarcodeLib.AlignmentPositions.CENTER;
b.LabelPosition = BarcodeLib.LabelPositions.BOTTOMCENTER;
b.ImageFormat = System.Drawing.Imaging.ImageFormat.Jpeg;//圖片格式
System.Drawing.Font font = new System.Drawing.Font("verdana",10f);//字體設置
b.LabelFont = font;
b.Height = height;//圖片高度設置(px單位)
b.Width = width;//圖片寬度設置(px單位)
image = b.Encode(type, code);//生成圖片
byte[] buffer = b.GetImageData(SaveTypes.GIF);//轉換byte格式
//byte轉換圖片格式
MemoryStream oMemoryStream = new MemoryStream(buffer);
//設定資料流位置
oMemoryStream.Position = 0;
//return buffer;
//return image;
return System.Drawing.Image.FromStream(oMemoryStream);
}
實例生成code128的條碼
System.Drawing.Image image;
int width=500, height=100;
image= GetBarcode(height, width, BarcodeLib.TYPE.CODE128, "004320000000JN60A____1242?10.0000144300", out image);
因為本人公司禁止上傳圖片格式文件,無法顯示最終結果
PS:本人第一次發博客,過於簡陋,請大家多多包含
附上:BarcodeLib下載地址: http://vdisk.weibo.com/s/iEw30
