2011-11-13 1 views
1

내 웹 사이트 중 하나에서 성능이 좋지 않아 고생하고 있습니다. 많은 이미지를 처리하고 있으며 이미지 처리기를 통해 이미지를 제공합니다. 내가 그것을 만들었을 때 나는 실수를했고 이미지를 다루기 위해 ASPX 파일을 만들었고 나는 generic handler (ASHX)를 사용해야했다.ASPX에서 asynchronus ASHX로 이미지 핸들러 변환

나는이 유망한 사이트를 찾았습니다. 비동기 이미지 처리기를 만드는 것입니다. 그러나 이것에 관해서는 거의 지식이 없기 때문에 도움이 필요합니다.

도움말 사이트 : 나는 핸들러 ShowImage.ashx 시작하지만 붙어 조금 메신저했다

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(); 
     } 
    } 
} 

: http://msdn.microsoft.com/en-us/magazine/cc163463.aspx

이 내 ShowImage.aspx 파일이 지금 모습입니다. 어떤 도움을 주셔서 감사합니다. 어디서 코드를 병합해야할지 모르겠다. 당신은 어떤 비동기을하지 않는

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

Hehe .. 당혹 스럽지만 사실입니다. 고맙습니다 :) – Martin