2

外部 Web サイトから画像を提供するハンドラーがあります。これは、それ自体で完全に正常に機能します。

しかし、画像の 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;
            }
        }
    }
}

理由はありますか?

4

1 に答える 1

-1

最後に追加してみてください:

Response.Flush();
Response.End();
于 2012-04-26T13:37:13.150 に答える