1、通过Nuget安装引用ZXing.Net
1)使用Nuget界面管理器
搜索“ZXing.Net.Bindings.ZKWeb.System.Drawing”
,在列表中分别找到它,点击"安装"
相关文档:VS(Visual Studio)中Nuget的使用
2)使用Package Manager命令安装
PM> Install-Package ZXing.Net.Bindings.ZKWeb.System.Drawing
3)使用.NET CLI命令安装
> dotnet add package ZXing.Net.Bindings.ZKWeb.System.Drawing
2、命名空间
using System;
using System.IO;
using System.DrawingCore;
using ZXing.Common;
using ZXing;
using ZXing.QrCode;
using ZXing.ZKWeb;
3、使用ZXing.Net生成条码图片
////// 生成条形码/// /// 条码信息/// 生成条码图片文件名/// 宽度/// 高度public static void CreateBarCode(string message, string gifFileName, int width, int height){ if (string.IsNullOrWhiteSpace(message)) { return; } var w = new ZXing.OneD.CodaBarWriter(); BitMatrix b = w.encode(message, BarcodeFormat.CODE_128, width, height); var zzb = new ZXing.ZKWeb.BarcodeWriter(); zzb.Options = new EncodingOptions() { Margin = 3, PureBarcode = false }; string dir = Path.GetDirectoryName(gifFileName); if (!Directory.Exists(dir)) { Directory.CreateDirectory(dir); } Bitmap b2 = zzb.Write(b); b2.Save(gifFileName, ImageFormat.Gif); b2.Dispose();}
4、使用ZXing.Net生成二维码图片
////// 生成二维码返回byte数组/// /// /// /// ///public static byte[] CreateCodeBytes(string message, int width = 600, int height = 600){ int heig = width; if (width > height) { heig = height; width = height; } if (string.IsNullOrWhiteSpace(message)) { return null; } var w = new QRCodeWriter(); BitMatrix b = w.encode(message, BarcodeFormat.QR_CODE, width, heig); var zzb = new BarcodeWriter(); zzb.Options = new EncodingOptions() { Margin = 0, }; Bitmap b2 = zzb.Write(b); byte[] bytes = BitmapToArray(b2); return bytes;}//将Bitmap 写为byte[]的方法public static byte[] BitmapToArray(Bitmap bmp){ byte[] byteArray = null; using (MemoryStream stream = new MemoryStream()) { bmp.Save(stream, ImageFormat.Png); byteArray = stream.GetBuffer(); } return byteArray;}