在数据库中保存条形码图像并检索它

时间:2015-08-05 17:56:01

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

我想在数据库中保存条形码图像。我的代码当前生成的只是一长串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);

}

1 个答案:

答案 0 :(得分:1)

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

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

作为对象,标签控件的默认字符串表示形式是类型的名称。您可能想要使用其.Text属性吗?

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

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