C# 以ASP.NET Web表单呈现条形码

C# 以ASP.NET Web表单呈现条形码,c#,asp.net,barcode,C#,Asp.net,Barcode,我正在尝试在asp.net页面中显示条形码。已下载带有示例代码的zen条形码渲染。我试过这个样品,我觉得很好用。一旦我尝试在我的代码条码标签显示为空。我检查了样本代码和我的我没有发现任何差异,只是数据传输是不同的。这就是我试过的 <barcode:BarcodeLabel ID="BarcodeLabel1" runat="server" BarcodeEncoding="Code39NC" LabelVerticalAlign="Bottom" Text="12345"></

我正在尝试在asp.net页面中显示条形码。已下载带有示例代码的zen条形码渲染。我试过这个样品,我觉得很好用。一旦我尝试在我的代码条码标签显示为空。我检查了样本代码和我的我没有发现任何差异,只是数据传输是不同的。这就是我试过的

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


if(!IsPostBack)
{  
列表symbologyDataSource=新列表(
Enum.GetNames(typeof(条形码符号学));
symbologyDataSource.Remove(“未知”);
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;
        }
BarcodeSymbology symbology=BarcodeSymbology.Unknown;
如果(条形码符号学.SelectedIndex!=0)
{
符号学=(条形码符号学)1;
}
符号学=(条形码符号学)1;
string text=hidID.Value.ToString();
字符串scaleText=“1”;
int标度;
如果(!int.TryParse(scaleText,out scale))
{
if(symbology==条形码symbology.CodeQr)
{
比例=3;
}
其他的
{
比例=1;
}
}
否则如果(刻度<1)
{
比例=1;
}
如果(!string.IsNullOrEmpty(text)&&symbology!=条形码symbology.Unknown)
{
barcodeRender.BarcodeEncoding=符号学;
barcodeRender.Scale=1;
Text=Text;
}

从下拉列表中将符号设置为Code39NC。比例为1,文本来自值传递的其他形式。bacodelable仍然只显示值,而不是条形码图片

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

这不是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上的产品页面链接:

我不熟悉这个库。试试zxing(斑马线)。它的工作原理可能与此类似,但在web表单中有不错的文档工作?是的。它会将您的内容输出为几种不同的条形码类型之一。我获取原始数据(字节[]),并将其保存为位图,然后将其作为一个资源使用。我已经创建了一个Javascript生成器。也许这可以帮助你解决你的需求。我想,我的代码现在开始工作了。让我来做吧。如果失败,我会尝试你的建议,我正在寻找二维条码。当然你的建议对我也很有用。谢谢你,格伦。我会尽力让你知道结果。
// 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()));
    }
}