2012-08-14 88 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