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

标签 c# asp.net barcode

我正在尝试在 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/

相关文章:

javascript - 如何在元素内使用 Javascript 绘制正方形?

android - 使用键盘后,Delphi 和 Honeywell Android Mobile Scanner 未填充

computer-vision - QR 码 - 相机方向/投影

java - 蓝牙条码扫描仪的解码输出

asp.net - 如何在sql server 2008中保存PDF、Docx、xls等文档

c# - 编写自定义 Msbuild 任务时抛出自定义编译器警告

c# - OAuth 不记名 token 身份验证未通过签名验证

c# - 什么是 WPF WebBrowser 控件的良好替代品?

c# - 泛型开放和封闭构造类型

c# - 导致索引和长度错误的子串