C# asp.net使用处理程序显示数据库中的图像(多于2个)

C# asp.net使用处理程序显示数据库中的图像(多于2个),c#,asp.net,sql,handler,datalist,C#,Asp.net,Sql,Handler,Datalist,Handler.ashx public void ProcessRequest (HttpContext context) { string imageid = context.Request.QueryString["ImID"]; SqlConnection connection = new SqlConnection(con); connection.Open(); SqlCommand command = new SqlC

Handler.ashx

public void ProcessRequest (HttpContext context) 
{
        string imageid = context.Request.QueryString["ImID"];
        SqlConnection connection = new SqlConnection(con);
        connection.Open();
        SqlCommand command = new SqlCommand("SELECT PhotoStoreTB.Data FROM PhotoStoreTB INNER JOIN UserTB ON UserTB.UserID = PhotoStoreTB.UserID WHERE PhotoStoreTB.UserID ='" + imageid + "'", connection);
        SqlDataReader dr = command.ExecuteReader();
        dr.Read();
        byte[] imagedata = (byte[])dr[0];
        context.Response.ContentType = "image";

        using (System.IO.MemoryStream str = new System.IO.MemoryStream(imagedata, true))
        {
            str.Write(imagedata, 0, imagedata.Length);
            Byte[] bytes = str.ToArray();
            context.Response.BinaryWrite(bytes);
        }
        connection.Close();
        context.Response.End();
   }
context.Response.End()引发异常

{Unable to evaluate expression because the code is optimized or a native frame is on top of the call stack.}
Aspx代码

<asp:Image ID="Image2" runat="server" ImageUrl='<%#"Handler.ashx?ImID="+ Eval("PUserID")%>'
                                                    Height="115px" Width="115px" CssClass="img-border"/>

请帮助我从数据库数据列表显示多个图像。请尝试使用
Response.Clear()在ProcessRequest函数的开头

<asp:Image ID="Image2" runat="server" ImageUrl='<%#"Handler.ashx?ImID="+ Eval("PUserID")%>'
                                                    Height="115px" Width="115px" CssClass="img-border"/>


您可以使用
ApplicationInstance.CompleteRequest
而不是
Response.Clear()

将您的响应内容类型从

  context.Response.ContentType = "image";

  context.Response.ContentType = "image/jpeg(png)"; depends on type
删除
context.Response.End()

请参见下面的示例代码

        byte[] image = imagefromDB();            
        context.Response.OutputStream.Write(image, 0, image.Length); 
        context.Response.ContentType ="image-mime-type"; 

我不能保证会有回应;不过这是个好主意。有时会出现故障。我知道,我试过了


尝试将jpg与图像响应一起使用,而不是jpeg。有时它也不起作用。jpg一直工作。

当您使用处理程序时,调用处理程序的每个实例都有自己的响应对象。因此,您可以从服务器代码绑定映像的动态路径

使用GridView并绑定RowDataBound事件,您可以非常轻松地完成此操作

protected void gvAlbumImages_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
           DataRow aRow = ((DataRowView)e.Row.DataItem).Row; 
           //This is the Row from your Datasource (which is a datatable)

          Image img =  (Image)e.Row[e.Row.RowIndex].FindControl("imgControl");
              //get the  Image object at the row you are binding to.
             //now simply set the source from the value in the DataRow
           img.ImageUrl = string.format("~/ImageHandler.ashx?ImageID={0}",aRow.Field<int>("ImageID")



         }
    }
protectedvoid gvalbummages\u RowDataBound(对象发送方,GridViewRowEventArgs e)
{
如果(e.Row.RowType==DataControlRowType.DataRow)
{
DataRow aRow=((DataRowView)e.Row.DataItem).Row;
//这是数据源(数据表)中的行
Image img=(Image)e.Row[e.Row.RowIndex].FindControl(“imgControl”);
//获取要绑定到的行的图像对象。
//现在只需从DataRow中的值设置源
img.ImageUrl=string.format(“~/ImageHandler.ashx?ImageID={0}”,aRow.Field(“ImageID”)
}
}

感谢您的回复,它没有给出任何异常,但仍然没有显示image@Amol,检查请求url并在Firebug/chrome控制台中查看响应状态。它获得了正确的响应url,但也没有显示图像感谢回复,但通过删除上下文它没有显示图像。response.End()它没有抛出任何异常,但仍然没有显示图像