我试图使用ZXing.NET为点网核心asp.net应用程序生成一个条形码。我不知道如何用条形码和文档来显示文本,似乎真的非常缺乏。有人知道怎么让它起作用吗?
这是我的代码(主要取自SO上的另一篇文章):
BarcodeWriterPixelData writer = new BarcodeWriterPixelData()
{
Format = BarcodeFormat.CODE_128,
Options = new EncodingOptions
{
Height = 400,
Width = 800,
PureBarcode = false, // this should indicate that the text should be displayed, in theory. Makes no difference, though.
Margin = 10
}
};
var pixelData = writer.Write("test text");
using (var bitmap = new Bitmap(pixelData.Width, pixelData.Height, System.Drawing.Imaging.PixelFormat.Format32bppRgb))
{
using (var ms = new System.IO.MemoryStream())
{
var bitmapData = bitmap.LockBits(new Rectangle(0, 0, pixelData.Width, pixelData.Height), System.Drawing.Imaging.ImageLockMode.WriteOnly, System.Drawing.Imaging.PixelFormat.Format32bppRgb);
try
{
System.Runtime.InteropServices.Marshal.Copy(pixelData.Pixels, 0, bitmapData.Scan0, pixelData.Pixels.Length);
}
finally
{
bitmap.UnlockBits(bitmapData);
}
bitmap.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
return File(ms.ToArray(), "image/jpeg");
}
}这给了我条形码,但没有内容。
或者,更好/更容易使用/更好地记录库的建议也会受到欢迎。
发布于 2019-01-07 07:39:53
您不需要手动将这些像素数据复制到另一个流中。总是喜欢使用接口提供的方法,即Save()方法。
public void YourActionMethod()
{
BarcodeWriter writer = new BarcodeWriter(){
Format = BarcodeFormat.CODE_128,
Options = new EncodingOptions {
Height = 400,
Width = 800,
PureBarcode = false,
Margin = 10,
},
};
var bitmap = writer.Write("test text");
bitmap.Save(HttpContext.Response.Body,System.Drawing.Imaging.ImageFormat.Png);
return; // there's no need to return a `FileContentResult` by `File(...);`
}演示:

发布于 2019-01-10 19:14:12
并不是所有可用的呈现器实现都支持条形码(f.e )下面内容的输出。PixelData渲染器不支持它)。您应该为不同的映像库使用特定的实现之一。例如,以下绑定提供程序、支持内容输出的呈现程序(和特定的BarcodeWriter):https://www.nuget.org/packages/ZXing.Net.Bindings.CoreCompat.System.Drawing https://www.nuget.org/packages/ZXing.Net.Bindings.Windows.Compatibility https://www.nuget.org/packages/ZXing.Net.Bindings.ZKWeb.System.Drawing https://www.nuget.org/packages/ZXing.Net.Bindings.SkiaSharp
https://stackoverflow.com/questions/54067764
复制相似问题