如何从数据库中检索二进制数据并在图像上显示

时间:2013-12-05 18:32:41

标签: asp.net-mvc

我想从数据库二进制文件中检索图像。但我不知道如何在Port Img1 img上显示

<img id="PrtImg1" alt="Product Image 1"  runat="server" 
                  class="picture"/>


if (sqlDataReader2["ProductColorType"].ToString() == "Type1")
                                    {
                                        Byte[] imgData = (byte[])sqlDataReader2["ProductImg"];
                                        Response.ContentType = (sqlDataReader2["ProductImg"].ToString());
                                        Response.OutputStream.Write(imgData, 0, imgData.Length);

1 个答案:

答案 0 :(得分:0)

试试这个:

的.aspx

<img id="PrtImg1" alt="Product Image 1" runat="server" class="picture" />

的.cs

if (sqlDataReader2["ProductColorType"].ToString() == "Type1")
{
    Byte[] imgData = (byte[])sqlDataReader2["ProductImg"];

    string base64String = string.Empty;

    try
    {
        base64String = System.Convert.ToBase64String(imgData, 0, imgData.Length);
    }
    catch (System.ArgumentNullException)
    {

    }

    this.PrtImg1.Src = String.Format("data:image/jpg;base64,{0}", base64String);
}