簡體   English   中英

在ASP.NET Web窗體中呈現條形碼

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

我想在asp.net頁面顯示條形碼。 已經下載了帶有示例代碼的zen條形碼渲染。 我試過這個樣本,它對我很好。 一旦我嘗試我的代碼條形碼標簽顯示為空。 我檢查了示例代碼並且我沒有發現任何差異,只有數據傳輸是不同的。 這是我試過的。

<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。 scale為1,文本來自其他形式,值也正在傳遞。 仍然是bacodelable只顯示值而不是條形碼圖片。

以下是使用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/

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM