【问题标题】:Barcode ASP.NET C# Issue条码 ASP.NET C# 问题
【发布时间】:2018-10-10 16:35:40
【问题描述】:

我有这个代码用于在页面加载时生成条形码。(下面的代码)txtBarcodeBlack.Text 内容从 SQL Server 数据库中提取为 varchar。 我有一个按钮提交,它将执行另一个过程,例如将 txtBarcodeBlack.text 保存到数据库。我的问题是当我按下按钮时,它给了我这个Problem 请帮忙。我被困了 2 天。

 private void loadBlackBarcode() {
    //barcode black
    //txtBarcodeBlack.Text = vBarcodeBlack;
    string barCodeBlack = txtBarcodeBlack.Text;
    System.Web.UI.WebControls.Image imgBarCodeBlack = new System.Web.UI.WebControls.Image();
    using (Bitmap bitMap = new Bitmap(barCodeBlack.Length * 40, 80))
    {
        using (Graphics graphics = Graphics.FromImage(bitMap))
        {
            Font oFont = new Font("IDAutomationHC39M", 16);
            PointF point = new PointF(2f, 2f);
            SolidBrush blackBrush = new SolidBrush(Color.Black);
            SolidBrush whiteBrush = new SolidBrush(Color.White);
            graphics.FillRectangle(whiteBrush, 0, 0, bitMap.Width, bitMap.Height);
            graphics.DrawString("*" + barCodeBlack + "*", oFont, blackBrush, point);
        }
        using (MemoryStream ms = new MemoryStream(100000))
        {
            bitMap.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
            byte[] byteImage = ms.ToArray();

            Convert.ToBase64String(byteImage);
            imgBarCodeBlack.ImageUrl = "data:image/png;base64," + Convert.ToBase64String(byteImage);
        }
        PlaceHolder1.Controls.Add(imgBarCodeBlack);
    }

}


<div class="col-xs-12 col-sm-12 col-lg-12" style="margin-bottom:20px;">
      <h3>Digital Black</h3>

      <asp:PlaceHolder ID="PlaceHolder1" runat="server"></asp:PlaceHolder>
</div>

【问题讨论】:

  • 只是猜测:也许barCodeBlack.Length 等于零。我想将零传递给 Bitmap ctor 会导致该错误。
  • 我试了调试,把这个barCodeBlack.Length;它显示 20. 表示它有长度。接下来我该怎么办?
  • 这是我唯一的猜测,抱歉。

标签: c# asp.net barcode


【解决方案1】:

更新 好的,我尝试寻找其他解决方案,我找到了答案。

我改变了这一行:

using (Bitmap bitMap = new Bitmap(barCodeBlack.Length * 40, 80))

到这一行:

using (Bitmap bitMap = new Bitmap(800, 80))

由于某种原因,新的位图不接受参数内的变量,所以我想我需要对条形码的测量值进行硬编码。

【讨论】:

    猜你喜欢
    • 2012-01-25
    • 1970-01-01
    • 2011-09-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-30
    • 2022-01-24
    • 1970-01-01
    相关资源
    最近更新 更多