简体   繁体   English

条码ASP.NET C#问题

[英]Barcode ASP.NET C# Issue

I have this code for generating barcode when page is loaded.(code below) txtBarcodeBlack.Text content is pulled from SQL Server Database as varchar. 我有此代码用于在页面加载时生成条形码。(下面的代码)txtBarcodeBlack.Text内容作为varchar从SQL Server数据库中提取。 I have a button submit that will do another process like saving the txtBarcodeBlack.text to database. 我有一个提交按钮,它将执行另一个过程,例如将txtBarcodeBlack.text保存到数据库。 My problem is when I press the button, it gives me this Problem please help. 我的问题是当我按下按钮时,它给了我这个问题,请帮忙。 I'm stuck for 2 days. 我被困了两天。

 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>

UPDATE Okay I tried searching for other solution, and I found the answer. 更新好的,我尝试搜索其他解决方案,然后找到了答案。

I changed this line: 我更改了这一行:

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

to this line: 到这行:

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

for some reason the new Bitmap does not accept variable inside the parameter, so I guess I need to hard code my measurement of the barcode. 由于某种原因,新的位图不接受参数内的变量,因此我想我需要对条形码的测量值进行硬编码。

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

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