public static string GetBarcode(string format, string value, int? width, int? height)
{
BarcodeWriter writer = null;
Bitmap bitmap = null;
string strbase64 = string.Empty;
writer = new BarcodeWriter();
if (format == "QR_CODE")
{
EncodingOptions options = new QrCodeEncodingOptions
{
DisableECI = true,
CharacterSet = "UTF-8",
Width = width == null ? 500 : Convert.ToInt32(width),
Height = height == null ? 500 : Convert.ToInt32(height)
};
writer.Options = options;
writer.Format = BarcodeFormat.QR_CODE;
bitmap = writer.Write(value);
}
else if (format == "CODE_39")
{
/*
* 1.支持數字,字母,-,$,空格
* 2.產生的條碼大
* 3.常用於許多用途
*/
writer.Format = BarcodeFormat.CODE_39;
MultiFormatWriter mutiWriter = new MultiFormatWriter();
BitMatrix bm = mutiWriter.encode(value, BarcodeFormat.CODE_39, (width == null ? 500 : Convert.ToInt32(width)), (height == null ? 150 : Convert.ToInt32(height)), null);
bitmap = new BarcodeWriter().Write(bm);
}
else if (format == "CODE_128")
{
/*
* 1.支持字母或者數字所有字符
* 2.包含3個表格更好的數據進行編碼
*/
writer.Format = BarcodeFormat.CODE_128;
MultiFormatWriter mutiWriter = new MultiFormatWriter();
BitMatrix bm = mutiWriter.encode(value, BarcodeFormat.CODE_128, (width == null ? 500 : Convert.ToInt32(width)), (height == null ? 150 : Convert.ToInt32(height)), null);
bitmap = new BarcodeWriter().Write(bm);
}
else if (format == "ITF")
{
EncodingOptions options = new QrCodeEncodingOptions
{
DisableECI = true,
CharacterSet = "UTF-8",
Width = width == null ? 500 : Convert.ToInt32(width),
Height = height == null ? 150 : Convert.ToInt32(height)
};
writer.Options = options;
writer.Format = BarcodeFormat.ITF;
bitmap = writer.Write(value);
}
using (MemoryStream ms = new MemoryStream())
{
bitmap.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
byte[] arr = new byte[ms.Length];
ms.Position = 0;
ms.Read(arr, 0, (int)ms.Length);
strbase64 = Convert.ToBase64String(arr);
}
return strbase64;
}
//引用文件下載地址
//http://files.cnblogs.com/files/guichi/Newtonsoft.rar