0

currently I'm using GenericHandler(.ashx) for displaying an image in image control.

See below code:

[1] .ASPX
    <asp:Image ID="Image1" runat="server" Width="350px" Height="415px" />
[2] .cs(codebehind)
    Image1.ImageUrl = string.Format("GridviewImage.ashx?ItemID={0}", itemID);

Now I need to get image of Image1 as a byte array (byte[]).

Is it possible?

pb2q
  • 58,613
  • 19
  • 146
  • 147
Pritesh
  • 3,208
  • 9
  • 51
  • 70
  • 2
    I asked a similar question here (includes code): http://stackoverflow.com/questions/1507572/streaming-databased-images-using-httphandler – IrishChieftain Oct 11 '12 at 03:44
  • Thanks for suggestion........i have seen `public bool IsReusable { get { return true; } }` ....but i don't know how to use it please post code snippet as answer thank you so much – Pritesh Oct 11 '12 at 04:13
  • How are you using a handler? All I see is a static image in the ASPX and I'm not sure what your question is? I'm posting a snippet that uses a handler to put an image in a GridView, hope this helps. – IrishChieftain Oct 11 '12 at 04:44

1 Answers1

1

If you have several images being streamed to the page, set the IsReusable to true. Use a DataReader for streaming the image back, not a DataSet like the other link.

<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;
            }
        }
    }
IrishChieftain
  • 15,108
  • 7
  • 50
  • 91
  • sorry if this is foolish question because i am newbie to asp.net...here `context.Response.BinaryWrite((Byte[])dr[0]);` is in `ImageHandler ` how could i retrieve from my codebehind(.cs) file...Thank you so much for answering – Pritesh Oct 11 '12 at 05:03
  • This code is a class and not your code-behind. The img tag at the top is in your ASPX form and makes an inline call to the handler class. – IrishChieftain Oct 11 '12 at 05:08
  • ok one more question...`ImageHandler ` will set image in `Image` control of `.aspx` page. after that if i want to get `byte array(byte[])` of Image which is now in `Image` control of `.aspx` page.....thank you so much – Pritesh Oct 11 '12 at 05:29
  • I don't understand what you're asking. The handler has streamed the image to the page and it displays. What else are you trying to do? – IrishChieftain Oct 11 '12 at 05:35
  • thank you so much...and sorry if i annoying you....but i want back the stream of image. i want to stream the image of image control(`Image1` which is in .ASPX page). – Pritesh Oct 11 '12 at 05:39
  • You've already got the image via the handler, so if you need to do something else with that image just call the handler to serve it up. – IrishChieftain Oct 11 '12 at 05:46
  • how to call handler to retrieve image which was displayed by handler in image control(`Image1` which is in `.ASPX` page)....thank you so much for replying – Pritesh Oct 11 '12 at 05:50
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/17848/discussion-between-irishchieftain-and-pritesh) – IrishChieftain Oct 11 '12 at 05:51