繁体   English   中英

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

[英]Convert a image handler from ASPX to asynchronus ASHX

我在自己的一个网站上表现不佳而苦苦挣扎。 它正在处理大量图像,我通过图像处理程序提供图像。 创建它时,我犯了一个错误,并创建了一个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);
    }
}

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

IHttpHandler派生,然后将您的代码放入ProcessRequest

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM