2012-08-14 69 views
0

以下HttpHandler将从数据库检索图像。当我最初测试它时,它工作正常。但现在的问题是,它仍然会检索图像,但不会显示在Listview中的图像控件中。HttpHandler问题,检索图像

`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(); 

     } 
    } 
} 

处理程序检索图像,但listview不显示它。

回答

0

尝试此方法,将内容类型设置为响应。

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

问候。

+0

哇..boom ...谢谢...它的工作...其实为什么这是问题?为什么要清除响应? – user1575229 2012-08-14 18:03:45

+0

您可以使用[** fiddler **](http://www.fiddler2.com/fiddler2/)查看您的输出结果。这样,您不仅会了解代码在响应中的变化,还会了解一个强大的工具。 – danielQ 2012-08-14 18:32:42

+0

谢谢..丹尼尔..真的很有帮助。 – user1575229 2012-08-14 22:08:28