Cie*_*eja 3 .net c# barcode zxing asp.net-core
我想在 ASP.NET 中使用 ZXing.NET 生成 EAN 13 条码并将其转换为 base64 字符串。
我在如何转换 BarcodeWriterPixelData 时遇到问题:
BarcodeWriterPixelData writer = new BarcodeWriterPixelData()
{
Format = BarcodeFormat.EAN_13
};
var pixelData = writer.Write(barcodeModel.BarcodeNumber);
Run Code Online (Sandbox Code Playgroud)
我正在尝试使用 ImageSharp
var base64String = string.Empty;
using (Image<Rgba32> image = Image.Load<Rgba32>(pixelData.Pixels))
{
base64String = image.ToBase64String();
}
Run Code Online (Sandbox Code Playgroud)
但它不起作用。
您可以使用它System.Drawing.Bitmap来执行此操作。添加对CoreCompat.System.Drawingnuget 包的引用(它处于测试阶段),然后使用以下代码:
BarcodeWriterPixelData writer = new BarcodeWriterPixelData()
{
Format = BarcodeFormat.EAN_13
};
var pixelData = writer.Write(barcodeModel.BarcodeNumber);
using (var bitmap = new System.Drawing.Bitmap(pixelData.Width, pixelData.Height, System.Drawing.Imaging.PixelFormat.Format32bppRgb))
{
using (var ms = new System.IO.MemoryStream())
{
var bitmapData = bitmap.LockBits(new System.Drawing.Rectangle(0, 0, pixelData.Width, pixelData.Height), System.Drawing.Imaging.ImageLockMode.WriteOnly, System.Drawing.Imaging.PixelFormat.Format32bppRgb);
try
{
// we assume that the row stride of the bitmap is aligned to 4 byte multiplied by the width of the image
System.Runtime.InteropServices.Marshal.Copy(pixelData.Pixels, 0, bitmapData.Scan0, pixelData.Pixels.Length);
}
finally
{
bitmap.UnlockBits(bitmapData);
}
// PNG or JPEG or whatever you want
bitmap.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
var base64str = Convert.ToBase64String(ms.ToArray());
}
}
Run Code Online (Sandbox Code Playgroud)
据我所知ImageSharp还没有发布。我根据这个答案推荐了 CoreCompact 。
| 归档时间: |
|
| 查看次数: |
3691 次 |
| 最近记录: |