.net generate barcode

1. .net standard library (.net standard 2.0)

  Nuget add references: ZXing.Net generates barcodes, ZXing.Net.Bindings.ImageSharp generates images

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;
        }

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325141466&siteId=291194637