【问题标题】:To save Barcode Image In Database and retrieve it将条形码图像保存在数据库中并检索它
【发布时间】:2015-10-28 15:29:39
【问题描述】:

我正在尝试将条形码图像保存在数据库中。我的代码当前生成的只是一长串 URL。我的代码如下:

string barCode = ds.Tables[0].Rows[0]["productId"].ToString();
System.Web.UI.WebControls.Image imgBarCode = new System.Web.UI.WebControls.Image();

using (Bitmap bitMap = new Bitmap(barCode.Length * 40, 80))
{
    using (Graphics graphics = Graphics.FromImage(bitMap))
    {
        // Font oFont = new Font("Free 3 of 9 Extended", 16);
        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("*" + barCode + "*", oFont, blackBrush, point);
    }

    using (MemoryStream ms = new MemoryStream())
    {
        bitMap.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
        byte[] byteImage = ms.ToArray();

        Convert.ToBase64String(byteImage);
        imgBarCode.ImageUrl = "data:image/png;base64," + Convert.ToBase64String(byteImage);

        **// I think Problem is here, in these lines //**          
        lblChkCopyIMAge.Text = imgBarCode.ToString();
    }

    placehod.Controls.Add(imgBarCode);
}

请任何人帮助我将完整的条形码图像保存在特定文件夹中并在数据库中保存相应的 URL,以便我可以进一步使用它。上面写的代码只是生成条形码图像和一个长字符串 URL。当我将它保存在数据库中时,它会将这个字符串保存在 URL 中:“System.Web.UI.WebControls.Label”。我已经使用nvarchar(max) 数据类型来保存ImageURL

我在数据库中保存的代码如下:

string strcmd5 = "update  product set symbology=" + "'" + "IDAutomationHC39M" + "'" + "," + "barcodeimage=" + "'" + lblChkCopyIMAge + "'" + "where productId=" + int.Parse(Label1.Text);

SqlCommand objsqlcmd5 = new SqlCommand(strcmd5, objsqlcon4);
int h = objsqlcmd5.ExecuteNonQuery();
objsqlcon4.Close();
if (h > 0)
{
    Response.Write("Record has submitted Successfully");
    ScriptManager.RegisterStartupScript(this, this.GetType(), "barcode", "alert('Saved Successfully');", true);

}

【问题讨论】:

    标签: c# asp.net image image-processing barcode


    【解决方案1】:

    您正在将值设置为标签控件:

    "barcodeimage=" + "'" + lblChkCopyIMAge + "'"
    

    作为一个对象,标签控件的默认字符串表示是类型的名称。您可能想改用它的.Text 属性?

    "barcodeimage=" + "'" + lblChkCopyIMAge.Text + "'"
    

    请注意,您还应该真的查看using parameterized queries 以进行数据库交互。就目前而言,此查询中使用的任何用户输入都是 SQL 注入漏洞

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-12-17
      • 1970-01-01
      • 2014-01-06
      • 2021-04-24
      • 2017-03-12
      • 1970-01-01
      相关资源
      最近更新 更多