条形码 ASP.NET C# 问题



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

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

我更改了这一行:

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

到这一行:

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

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

最新更新