2012-04-26 13 views
2

我有一個處理程序,它提供來自外部網站的圖像。這個工作絕對很好。圖片src不顯示由處理程序提供的外部圖像

但是,當我在圖像src屬性中使用處理程序它不起作用。

這是調用我作出了處理:

<img src="myhandler.ashx?image=http://nssdc.gsfc.nasa.gov/image/planetary/venus/gal_venus_37218.jpg"/> 

這是處理程序的代碼:

using System; 
using System.Collections; 
using System.Data; 
using System.Linq; 
using System.Web; 
using System.Web.Services; 
using System.Web.Services.Protocols; 
using System.Xml.Linq; 
using System.Drawing; 
using System.Net; 
using System.IO; 
using System.Drawing.Imaging; 

namespace MumsChoice.Portal.UI.Handlers 
{ 
    /// <summary> 
    /// Summary description for $codebehindclassname$ 
    /// </summary> 
    [WebService(Namespace = "http://tempuri.org/")] 
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] 
    public class ExternalImageHandler : IHttpHandler 
    { 

     public void ProcessRequest(HttpContext context) 
     { 
      Bitmap bitOutput = null; 
      Stream stream = null; 
      string contentType = string.Empty; 
      try 
      { 
       WebRequest req = WebRequest.Create(context.Request.QueryString["image"]); 
       WebResponse response = req.GetResponse(); 
       contentType = response.ContentType; 
       stream = response.GetResponseStream(); 
       bitOutput = new Bitmap(stream); 
      } 
      catch 
      { 
       bitOutput.Dispose(); 
      } 
      finally 
      { 
       stream.Close(); 
       stream.Dispose(); 
       stream = null; 
      } 

      ImageFormat format; 
      switch (contentType) 
      { 
       case "image/png": 
        format = ImageFormat.Png; 
        break; 
       case "image/gif": 
        format = ImageFormat.Gif; 
        break; 
       default: 
        format = ImageFormat.Jpeg; 
        break; 
      } 

      context.Response.ContentType = contentType; 
      bitOutput.Save(context.Response.OutputStream, format); 
      bitOutput.Dispose(); 
      return; 
     } 


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

任何想法,爲什麼?

+0

檢查與Firebug的NET控制檯,在開發工具鉻合金網片或招潮蟹什麼瀏覽器實際請求 – madaboutcode 2012-04-26 13:36:09

+0

是否webconfig知道新的處理程序服務器? http://msdn.microsoft.com/en-us/library/system.web.ihttphandler.processrequest(v=vs.80).aspx – rt2800 2012-04-26 13:38:02

+0

我認爲你需要檢查是否有拋出錯誤,調試到看看你是否得到了圖像併發送它,那麼如果所有的工作和問題都在img標籤上,那麼在你的情況下,我將使用URLEncode對http://進行編碼。此外,我將remvoe WebService和WebServiceBinding這不屬於處理程序。 – Aristos 2012-04-26 13:45:04

回答

-1

嘗試加入底:

Response.Flush(); 
Response.End(); 
+0

,這沒有什麼區別 – user441365 2012-04-26 13:40:34