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);
}
}
嘿嘿..尷尬,但真實。謝謝 :) – Martin