【问题标题】:Convert a image handler from ASPX to asynchronus ASHX将图像处理程序从 ASPX 转换为异步 ASHX
【发布时间】:2011-11-13 16:11:53
【问题描述】:

我在我的一个网站上因性能不佳而苦苦挣扎。它正在处理很多图像,我通过图像处理程序提供图像。当我创建它时,我犯了一个错误并创建了一个 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);
    }
}

【问题讨论】:

    标签: asp.net asynchronous httphandler


    【解决方案1】:

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

    IHttpHandler 派生,只需将您的代码放入ProcessRequest

    【讨论】:

    • 呵呵..尴尬但真实。谢谢你:)
    猜你喜欢
    • 1970-01-01
    • 2012-01-08
    • 2012-04-30
    • 1970-01-01
    • 2012-02-02
    • 1970-01-01
    • 2013-09-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多