简体   繁体   English

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

[英]Render a barcode in ASP.NET Web Form

i am trying to show the barcode in asp.net page. 我想在asp.net页面显示条形码。 already download the zen barcode render with sample code. 已经下载了带有示例代码的zen条形码渲染。 i tried the sample it is working fine with me. 我试过这个样本,它对我很好。 once i try in my code barcode label is showing empty. 一旦我尝试我的代码条形码标签显示为空。 i checked with sample code and mine i did not find any difference , only data transfer is the different. 我检查了示例代码并且我没有发现任何差异,只有数据传输是不同的。 this is what i tried. 这是我试过的。

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

this is the function 这是功能

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

symbology is set as Code39NC from the dropdown. 符号系统从下拉列表中设置为Code39NC。 scale is 1 and text is coming from other form the value is passing as well. scale为1,文本来自其他形式,值也正在传递。 still the bacodelable is showing only value not the barcode picture. 仍然是bacodelable只显示值而不是条形码图片。

Here are two code samples using ZXing to create a (QR) barcode as both an image and as a base64 encoded string. 以下是使用ZXing创建(QR)条形码作为图像和base64编码字符串的两个代码示例。 Both of these options can be used with an <img /> tag to embed the barcode in the page. 这两个选项都可以与<img />标签一起使用,以在页面中嵌入条形码。

This is not an ASP.NET control. 这不是ASP.NET控件。 It is a library that creates barcodes from text. 它是一个从文本创建条形码的库。

// 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()));
    }
}

Hope this helps! 希望这可以帮助! Happy coding. 快乐的编码。

UPDATE: Here is the link to their product page on codeplex: https://zxingnet.codeplex.com/ 更新:以下是codeplex产品页面的链接: https ://zxingnet.codeplex.com/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM