1、.net 标准库(.net standard 2.0)
Nuget添加引用:ZXing.Net生成条形码,ZXing.Net.Bindings.ImageSharp生成图片
public static string GenerateBarcode(string barcode) { var barcodeWriter = new ZXing.ImageSharp.BarcodeWriter<Rgba32> { Format = BarcodeFormat.CODE_128, Options = new EncodingOptions { Height = 100, Width = 200, PureBarcode = false } }; var name = Guid.NewGuid().ToString().Replace("-", "") + ".png"; var localRes = AppSettingsHelper.LocalRes; var filePath = localRes + name; using (var image = barcodeWriter.Write(barcode)) { image.Save(filePath); } var resUrl = AppSettingsHelper.ResUrl; var url = filePath.Replace(localRes, resUrl); return url; }
2、.net framework 4.6.1
public static string GenerateBarcode(string barcode) { BarcodeWriter writer = new BarcodeWriter(); writer.Format = BarcodeFormat.CODE_128; writer.Options = new ZXing.Common.EncodingOptions { Height = 100, Width = 200, PureBarcode = false }; Bitmap bitmap = writer.Write(barcode); var fileName = Guid.NewGuid().ToString().Replace("-", "") + ".png"; var localRes = AppSettingsHelper.LocalRes;var filePath = localRes + fileName; bitmap.Save(filePath); var resUrl = AppSettingsHelper.ResUrl; var url = filePath.Replace(localRes, resUrl); return url; }