无法在GridView中显示图像

本文关键字:显示 显示图 图像 GridView | 更新日期: 2023-09-27 18:03:34

我无法显示gridview中数据库中的图像。我能拯救他们。当我试图检索它不显示任何东西,但只是一个产品名称。

这是我的GridView

<Columns>
        <asp:CommandField ShowDeleteButton="True" />
        <asp:BoundField DataField="ProductName" HeaderText="ProductName" SortExpression="ProductName" />
         <asp:ImageField DataImageUrlField = "ProductImagesID"
            DataImageUrlFormatString = "../ImageHandler.ashx?ProductImagesID={0}"
            ControlStyle-Width = "75" ControlStyle-Height = "75"
            HeaderText = "Preview Image">
                <ControlStyle Height="100px" Width="100px"></ControlStyle>
            </asp:ImageField>
    </Columns>

图像处理程序:

public void ProcessRequest(HttpContext context)
    {
        Byte[] productImage;
        if (context.Request.QueryString["ProductImageId"] != null)
        {
            int productImageId = Convert.ToInt32(context.Request.QueryString["ProductImageId"]);
            productImage = ProductImageBL.GetImage(productImageId);
            if (productImage != null)
            {
                context.Response.BinaryWrite(productImage);
                context.Response.End();
            }
        }
    }

ProductImageBL GetImage方法:

public static Byte[] GetImage(int productImageId)
    {
        SqlCommand cmd = new SqlCommand();
        cmd.CommandText = "Select ProductImage from ProductImages where ProductImageId = @ProductImageId";
        cmd.Parameters.AddWithValue("@ProductImageId", SqlDbType.Int).Value = productImageId;
        SqlDataReader dataReader = DbUtility.GetDataReader(cmd);
        if (dataReader.HasRows)
        {
            dataReader.Read();
            return (Byte[])dataReader[0];
        }
        else
        {
            return null;
        }
    }

请帮助我这个代码。

无法在GridView中显示图像

为图片创建一个页面,命名为GetMeImage.aspx,功能如下

protected void Page_Load(object sender, EventArgs e)
{
    if (Request.QueryString["ProductImagesID"] != null)
    {
string query = "Select ProductImage from ProductImages where ProductImageId = @ProductImageId";
SqlCommand cmd = new SqlCommand(query,YourConnectionObject);
    cmd.Parameters.AddWithValue("@ProductImageId", SqlDbType.Int).Value = Convert.ToInt32(Request.QueryString["ProductImagesID"]);
    SqlDataAdapter adapter = new SqlDataAdapter(cmd);
    DataTable dt = new DataTable();
    if (dt != null)
    {
        Byte[] bytes = (Byte[])dt.Rows[0]["Data"];
        Response.Buffer = true;
        Response.Charset = "";
        Response.Cache.SetCacheability(HttpCacheability.NoCache);
        Response.ContentType = dt.Rows[0]["ContentType"].ToString();
        Response.AddHeader("content-disposition", "attachment;filename="
        + dt.Rows[0]["Name"].ToString());
        Response.BinaryWrite(bytes);
        Response.Flush();
        Response.End();
    }
}
}

在gridview添加模板字段

<asp:ImageField DataImageUrlField = "ProductImagesID"
            DataImageUrlFormatString = "../GetMeImage.aspx?ProductImagesID={0}"
            ControlStyle-Width = "75" ControlStyle-Height = "75"
            HeaderText = "Preview Image">
                <ControlStyle Height="100px" Width="100px"></ControlStyle>
            </asp:ImageField>