将图像处理程序从ASPX转换为异步ASHX

时间:2011-11-13 16:11:54

标签: asp.net asynchronous httphandler

我在我的一个网站上遇到了糟糕的表现。它正在处理大量图像,我通过图像处理程序提供图像。当我创建它时,我犯了一个错误并创建了一个ASPX文件来处理图像,我应该使用通用处理程序(ASHX)。

我发现这个好网站看起来很有前景。它是关于创建一个异步图像处理程序。但我对此知之甚少,需要一些帮助。

帮助网站: http://msdn.microsoft.com/en-us/magazine/cc163463.aspx

这就是我的ShowImage.aspx文件现在的样子:

using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using System.Configuration;
using System.Drawing;
using System.Drawing.Imaging;
using System.Web.Caching;
using System.Collections;

public partial class ShowImage : System.Web.UI.Page
{
    Gallery gal = new Gallery();

    private void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            // Start with empty Response object.
            Response.Clear();

            // Get the image path from the URL.
            string imagePath = Request.QueryString["image"];

            // Set the size of the image
            int maxHeight = 1000;
            if (Request.QueryString["maxHeight"] != null)
            {
                string mh = Request.QueryString["maxHeight"];
                maxHeight = int.Parse(mh);
            }

            int maxWidth = 1000;
            if (Request.QueryString["maxWidth"] != null)
            {
                string mw = Request.QueryString["maxWidth"];
                maxWidth = int.Parse(mw);
            }

            string thumbPath = gal.ThumbnailPath(Server.MapPath(imagePath), maxHeight, maxWidth);

            byte[] buffer = null;
            System.Drawing.Image img = System.Drawing.Image.FromFile(thumbPath);
            using (MemoryStream ms = new MemoryStream())
            {
                img.Save(ms, img.RawFormat);
                buffer = ms.ToArray();
            }

            Response.ContentType = "image/" + Path.GetExtension(thumbPath).Remove(0, 1);
            Response.OutputStream.Write(buffer, 0, buffer.Length);
            img.Dispose();
            Response.End();
        }
    }
}

我已经开始使用处理程序ShowImage.ashx,但我有点卡住了。任何帮助表示赞赏。我不知道我应该把代码合并到哪里。

using System;
using System.Web;
using System.Drawing;
using System.Drawing.Imaging;
using System.Threading;
using System.IO;

public class ShowImage : IHttpAsyncHandler
{
    //private ShowImageService _ts;
    private ShowImageServiceAsyncResult _ar;
    private HttpContext _context;
    private Exception _ex;

    public void ProcessRequest (HttpContext context)
    {
        // Never used
    }

    public bool IsReusable { get { return false; } }

    public IAsyncResult BeginProcessRequest(HttpContext context, AsyncCallback cb, object state)
    {
        _context = context;

        _ar = new ShowImageServiceAsyncResult(cb, state);

        // SHOULD I PLACE CODE HERE?

        return _ar;
    }

    public void EndProcessRequest(IAsyncResult ar)
    {
        if (_ex != null)
        {
            // If an exception was thrown, rethrow it
            throw _ex;
        }
        else
        {
            // Otherwise return the generated image
        }
    }
}

class ShowImageServiceAsyncResult : IAsyncResult
{
    private AsyncCallback _cb;
    private object _state;
    private ManualResetEvent _event;
    private bool _completed = false;
    private object _lock = new object();

    public ShowImageServiceAsyncResult(AsyncCallback cb, object state)
    {
        _cb = cb;
        _state = state;
    }

    public Object AsyncState { get { return _state; } }

    public bool CompletedSynchronously { get { return false; } }

    public bool IsCompleted { get { return _completed; } }

    public WaitHandle AsyncWaitHandle
    {
        get
        {
            lock (_lock)
            {
                if (_event == null)
                    _event = new ManualResetEvent(IsCompleted);
                return _event;
            }
        }
    }

    public void CompleteCall()
    {
        lock (_lock)
        {
            _completed = true;
            if (_event != null) _event.Set();
        }

        if (_cb != null) _cb(this);
    }
}

1 个答案:

答案 0 :(得分:0)

您没有进行任何异步调用来检索您的Image,因此您不需要异步处理程序。

IHttpHandler导出,只需将您的代码放入ProcessRequest