C# 以字节数组的形式检索图像控件的图像,该字节数组是使用generichandler(.ashx)设置的?

C# 以字节数组的形式检索图像控件的图像,该字节数组是使用generichandler(.ashx)设置的?,c#,asp.net,image,generic-handler,C#,Asp.net,Image,Generic Handler,目前我正在使用GenericHandler.ashx在图像控件中显示图像 见以下代码: [1] .ASPX <asp:Image ID="Image1" runat="server" Width="350px" Height="415px" /> [2] .cs(codebehind) Image1.ImageUrl = string.Format("GridviewImage.ashx?ItemID={0}", itemID); 现在我需要将Image1的图像作为

目前我正在使用GenericHandler.ashx在图像控件中显示图像

见以下代码:

[1] .ASPX
    <asp:Image ID="Image1" runat="server" Width="350px" Height="415px" />
[2] .cs(codebehind)
    Image1.ImageUrl = string.Format("GridviewImage.ashx?ItemID={0}", itemID);
现在我需要将Image1的图像作为字节数组byte[]获取


是否可能?

如果有多个图像流式传输到页面,请将IsReusable设置为true。使用DataReader将图像流回,而不是像其他链接那样使用数据集

<img src='ImageHandler.ashx?ProductID=<%# Eval("ProductID")%>' alt="<%# Eval("ProductName") %>" 
    title="<%# Eval("ProductName") %>" />

    public class ImageHandler : IHttpHandler
    {
        public void ProcessRequest(HttpContext context)
        {
            if (context.Request.QueryString["productID"] != null)
            {
                try
                {
                    string ProductID = context.Request.QueryString["ProductID"];
                    if (Convert.ToInt32(ProductID) > 0)
                    {
#if DEBUG
      const string CONN = "Initial Catalog=db1;Data Source=server1;Integrated Security=SSPI;";
#else
      const string CONN = "server=server2;database=db2;uid=un2;pwd=pw2;";
#endif
                        string selectQuery = "SELECT Photo FROM dbo.Products WHERE dbo.Products.ProductID=" + ProductID.ToString();
                        SqlConnection conn = new SqlConnection(CONN);
                        SqlCommand cmd = new SqlCommand(selectQuery, conn);

                        conn.Open();
                        SqlDataReader dr = cmd.ExecuteReader();

                        dr.Read();
                        context.Response.BinaryWrite((Byte[])dr[0]);
                        dr.Close();
                        conn.Dispose();
                        // context.Response.End(); --> caused an "Abort thread" error - this is correct and is a special exception
                    }
                }
                catch (Exception ex)
                {
                    ErrorReporting.LogError(ex);   
                }
            }
            else
                throw new ArgumentException("No ProductID parameter specified");
        }

        public bool IsReusable
        {
            get
            {
                return true;
            }
        }
    }

我在这里问了一个类似的问题,包括代码:谢谢你的建议…….我已经看到公共bool是可重用的{get{return true;}}}…但我不知道如何使用它。请发布代码片段作为答案谢谢你这么多你是如何使用处理程序的?我看到的只是ASPX中的静态图像,我不确定你的问题是什么?我正在发布一个使用处理程序将图像放入GridView的代码段,希望这有帮助。如果这是一个愚蠢的问题,请道歉,因为我是asp.net的新手…此处为context.Response.BinaryWriteByte[]dr[0];在ImageHandler中如何从codebehind.cs文件中检索…非常感谢您的回答此代码是一个类,而不是您的代码隐藏。顶部的img标记位于您的ASPX表单中,并对处理程序类进行内联调用。好的,还有一个问题…ImageHandler将在.ASPX页面的图像控件中设置图像。在此之后,如果我想获取图像的字节数组字节[],它现在位于.aspx页面的图像控制中……谢谢你,我不明白你在问什么。处理程序已将图像流式传输到页面并显示。你还想做什么?非常感谢你…如果我打扰了你,我很抱歉…但是我想要回图像流。我想对.ASPX页面中的image controlImage1的图像进行流式处理。