asp.net 使用处理程序显示数据库中的图像(超过 2 张)

本文关键字:图像 超过 数据库 net 处理 显示 程序 asp | 更新日期: 2023-09-27 18:35:19

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"/>

我想在数据列表中显示多个图像

数据列表绑定

 try
    {
        ld.Openconnection();
        SqlCommand Cmd = new SqlCommand("PGetPropertyByCriteria", ld.con);
        Cmd.Parameters.AddWithValue("@StartIndex", 1);
        Cmd.Parameters.AddWithValue("@EndIndex", 10);
        Cmd.Parameters.AddWithValue("@flag", "Get");
        Cmd.CommandType = CommandType.StoredProcedure;
        SqlDataAdapter SqlAda = new SqlDataAdapter(Cmd);
        DataSet DsStudentDetails = new DataSet();
        SqlAda.Fill(DsStudentDetails);
        if (DsStudentDetails.Tables.Count > 0 && DsStudentDetails.Tables[0].Rows.Count > 0)
        {
            TotalPage = (Int32.Parse(DsStudentDetails.Tables[0].Rows[0]["row"].ToString()) / PageSize) + ((Int32.Parse(DsStudentDetails.Tables[0].Rows[0]["row"].ToString()) % PageSize) > 0 ? 1 : 0);
            CurrentRecord = DsStudentDetails.Tables[0].Rows.Count;
            DataTable tDataTable = new DataTable("PagingTable");
            tDataTable.Columns.Add(new DataColumn("LinkButtonVisible", typeof(bool)));
            tDataTable.Columns.Add(new DataColumn("DisplayName", typeof(string)));
            tDataTable.Columns.Add(new DataColumn("Value", typeof(string)));
            tDataTable.Columns.Add(new DataColumn("LabelVisible", typeof(bool)));

            dtlProduct.DataSource = DsStudentDetails.Tables[0];
            dtlProduct.DataBind();
        }
        else
        {
            DLPAGING.DataSource = null;
            DLPAGING.DataBind();
            dtlProduct.DataSource = null;
            dtlProduct.DataBind();
        }
    }
    catch (Exception ex)
    {
        ex.ToString();
    }
    finally
    {
        ld.Closeconnection();
    }

请帮助我从数据库中显示多个图像到数据列表

asp.net 使用处理程序显示数据库中的图像(超过 2 张)

尝试在 ProcessRequest 函数的开头使用 Response.Clear();

您可以使用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"; 

我不能保证 Response.CLear(); 是个好主意。有时它会出现故障。我知道,我试过了。

尝试将 JPG 与图像响应一起使用,而不是 JPEG。有时它也不起作用。JPG一直有效。

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

使用 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")

         }
    }