gpt4 book ai didi

c# - 在 ASP.NET Web 窗体中呈现条形码

转载 作者:行者123 更新时间:2023-11-30 16:49:39 24 4
gpt4 key购买 nike

我正在尝试在 asp.net 页面中显示条形码。已经下载带有示例代码的 zen 条形码渲染器。我尝试了 sample ,它对我来说工作正常。一旦我在我的代码中尝试,条形码标签显示为空。我检查了示例代码,我没有发现任何区别,只有数据传输不同。这是我试过的。

<barcode:BarcodeLabel ID="BarcodeLabel1" runat="server" BarcodeEncoding="Code39NC" LabelVerticalAlign="Bottom" Text="12345"></barcode:BarcodeLabel>

 if (!IsPostBack)
{
List<string> symbologyDataSource = new List<string>(
Enum.GetNames(typeof(BarcodeSymbology)));
symbologyDataSource.Remove("Unknown");
barcodeSymbology.DataSource = symbologyDataSource;
barcodeSymbology.DataBind();
}

这是函数

BarcodeSymbology symbology = BarcodeSymbology.Unknown;

if (barcodeSymbology.SelectedIndex != 0)
{
symbology = (BarcodeSymbology)1;
}
symbology = (BarcodeSymbology)1;
string text = hidID.Value.ToString();

string scaleText = "1";
int scale;
if (!int.TryParse(scaleText, out scale))
{
if (symbology == BarcodeSymbology.CodeQr)
{
scale = 3;
}
else
{
scale = 1;
}
}
else if (scale < 1)
{
scale = 1;
}

if (!string.IsNullOrEmpty(text) && symbology != BarcodeSymbology.Unknown)
{
barcodeRender.BarcodeEncoding = symbology;
barcodeRender.Scale = 1;
barcodeRender.Text = text;
}

符号系统从下拉列表中设置为 Code39NC。比例为 1,文本来自其他形式,该值也正在传递。 bacodelable 仍然只显示值(value)而不是条形码图片。

最佳答案

这里有两个使用 ZXing 的代码示例创建一个 (QR) 条形码作为图像和 base64 编码的字符串。这两个选项都可以与 <img /> 一起使用标记以在页面中嵌入条形码。

这不是 ASP.NET 控件。它是一个从文本创建条形码的库。

// First Text to QR Code as an image
public byte[] ToQRAsGif(string content)
{
var barcodeWriter = new BarcodeWriter
{
Format = BarcodeFormat.QR_CODE,
Options = new EncodingOptions
{
Height = this._h,
Width = this._w,
Margin = 2
}
};

using (var bitmap = barcodeWriter.Write(content))
using (var stream = new MemoryStream())
{
bitmap.Save(stream, ImageFormat.Gif);
stream.Position = 0;
return stream.GetBuffer();
}
}

// From Text to QR Code as base64 string
public string ToQRAsBase64String(string content)
{
var barcodeWriter = new BarcodeWriter
{
Format = BarcodeFormat.QR_CODE,
Options = new EncodingOptions
{
Height = _h,
Width = _w,
Margin = 2
}
};

using (var bitmap = barcodeWriter.Write(content))
using (var stream = new MemoryStream())
{
bitmap.Save(stream, ImageFormat.Gif);
return String.Format("data:image/gif;base64,{0}", Convert.ToBase64String(stream.ToArray()));
}
}

希望对您有所帮助!快乐编码。

更新:这是他们在 codeplex 上的产品页面的链接:https://zxingnet.codeplex.com/

关于c# - 在 ASP.NET Web 窗体中呈现条形码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36291377/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com