2010年8月2日 星期一

ashx Handler output image/PNG with cache

public void ProcessRequest(HttpContext context)  {     if (!String.IsNullOrEmpty(context.Request.QueryString["station"]))   {    if (context.Cache["BMPCache" + context.Request.QueryString["station"]] != null)    {     context.Response.ContentType = "image/png";     MemoryStream stream2 = new MemoryStream();     //img.Save(stream, ImageFormat.Png);     Bitmap temp = (Bitmap)context.Cache["BMPCache" + context.Request.QueryString["station"]];     temp.Save(stream2, ImageFormat.Png);     stream2.WriteTo(context.Response.OutputStream);    }    else    {      Bitmap img;      ......繪圖程式碼略................      context.Response.ContentType = "image/png";     MemoryStream stream = new MemoryStream();     img.Save(stream, ImageFormat.Png);    // context.Cache["BMPCache" + context.Request.QueryString["station"]] = img;
    Cache.Insert("BMPCache" + context.Request.QueryString["station"], img, null, DateTime.UtcNow.AddSeconds(10), System.Web.Caching.Cache.NoSlidingExpiration);
    stream.WriteTo(context.Response.OutputStream);    }   }   else   {    context.Response.ContentType = "text/html";    context.Response.Write("

Need a valid id

"
); } }

沒有留言: