2011-11-13 38 views
1

Im在我的某个网站上性能不佳。它处理大量图像,并通过图像处理程序提供图像。当我创建它时,我犯了一个错误,并创建了一个ASPX文件来处理图像,我应该使用通用处理程序(ASHX)。将图像处理程序从ASPX转换为asynchronus 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); 
    } 
} 

回答

0

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

衍生自IHttpHandler,只需将您的代码放入ProcessRequest即可。

+0

嘿嘿..尴尬,但真实。谢谢 :) – Martin

相关问题