0

The following HttpHandler is to retrieve image from database. When I initially tested it it was working fine. but now the problem is that it still retieve image but does not display it in image control that is in Listview.

    `public void ProcessRequest (HttpContext context) 
{
    string imageid = context.Request.QueryString["ImID"];
    using (SqlConnection connection = ConnectionManager.GetConnection())
    {
        SqlCommand command = new SqlCommand("select Normal_Thumbs from User_Images where Id=" + imageid, connection);
        SqlDataReader dr = command.ExecuteReader();
        dr.Read();
        if (dr[0] != DBNull.Value)
        {
            Stream str = new MemoryStream((Byte[])dr[0]);

            Bitmap Photo = new Bitmap(str);
            int Width = Photo.Width;
            int Height = Photo.Height;
            int imagesize = 200;
            if (Photo.Width > Photo.Height)
            {
                Width = imagesize;
                Height = Photo.Height * imagesize / Photo.Width;
            }
            else
            {
                Width = Photo.Width * imagesize / Photo.Height;
                Height = imagesize;
            }

            Bitmap bmpOut = new Bitmap(150, 150);

            Graphics g = Graphics.FromImage(bmpOut);
            g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
            g.FillRectangle(Brushes.White, 0, 0, Width, Height);
            g.DrawImage(Photo, 0, 0, Width, Height);

            MemoryStream ms = new MemoryStream();
            bmpOut.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
            byte[] bmpBytes = ms.GetBuffer();
            bmpOut.Dispose();
            ms.Close();

            context.Response.Write(bmpBytes);

            context.Response.End();

        }
    }
}

The Handler Retrieves the image but the listview does not display it.

azam
  • 205
  • 4
  • 20

1 Answers1

0

Try this approach, setting content type to the response.

...
    context.Response.Clear();
    context.Response.ContentType = System.Drawing.Imaging.ImageFormat.Png.ToString();
    context.Response.OutputStream.Write(bmpBytes, 0, bmpBytes.Length);             
    context.Response.End(); 
...

Regards.

danielQ
  • 2,016
  • 1
  • 15
  • 19
  • wow..boom... Thank you... It worked... Actually why is this the problem??? why to clear the Response? – azam Aug 14 '12 at 18:03
  • You can check out your output with [**fiddler**](http://www.fiddler2.com/fiddler2/). That way you'll not only understand what does code change on your response, but get to know a powerful tool. – danielQ Aug 14 '12 at 18:32