关于ZXing 就不用多介绍了,本问主要介绍如何使用ZXing 生成条形码和二维码的图片。
Release 版本的dll下载地址:http://zxingnet.codeplex.com/
下载完成后在项目中引用ZXing.dll,然后做以下封装:
/// <summary>
/// 产生二维码
/// </summary>
/// <param name="width">二维码的宽度</param>
/// <param name="height">二维码的高度</param>
/// <param name="Contents">二维码内容</param>
/// <returns>生成的图片</returns>
public static Bitmap CreateQRCode( string Contents,int width = 430, int height = 430)
{
EncodingOptions options = null;
BarcodeWriter writer = null;
options = new QrCodeEncodingOptions
{
DisableECI = true,
CharacterSet = "UTF-8",
Width = width,
Height = height
};
writer = new BarcodeWriter();
writer.Format = BarcodeFormat.QR_CODE;
writer.Options = options;
Bitmap bitmap = writer.Write(Contents);
return bitmap;
}
/// <summary>
/// 产生条形码
/// </summary>
/// <param name="width">条形码图片的宽度</param>
/// <param name="height">条形码图片的高度</param>
/// <param name="Contents">条形码内容</param>
/// <returns>生成的图片</returns>
public static Bitmap CreateBarCode( string Contents,int width=530, int height=200)
{
EncodingOptions options =null;
BarcodeWriter writer =null;
options = new QrCodeEncodingOptions
{
DisableECI = true,
CharacterSet = "UTF-8",
Width = width,
Height = height
};
writer = new BarcodeWriter();
writer.Format = BarcodeFormat.CODE_128;
writer.Options = options;
Bitmap bitmap = writer.Write(Contents);
return bitmap;
}
注意在生成条形码的时候 设置好合适的BarcodeFormat
如果您觉得我的文章对您有所帮助,欢迎扫码进行赞赏!