Image Thumbnail HttpHandler
The real work is done in Image helper.
This handler uses querystrings (img.ashx?name=file.jpg&size=1). Filename is passed through Path.GetFileName to ensure there's no shifting directories, and we check File.Exists. You could of course make size the actual width for full flexibility.
<%@ WebHandler Language="C#" Class="img" %>
using System.IO;
using System.Web;
public class img : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
string name = context.Request.QueryString["name"];
string size = context.Request.QueryString["size"];
if (string.IsNullOrEmpty(name)) return;
string filepath = context.Server.MapPath(@"~/images/");
filepath = Path.Combine(filepath, Path.GetFileName(name));
if (!File.Exists(filepath)) return;
switch (size)
{
case "1": //small
ImageHelper.WriteThumbnailSimple(context, filepath, 64);
break;
case "2": //medium
ImageHelper.WriteThumbnail(context, filepath, 200);
break;
default: //original
ImageHelper.WriteImage(context, filepath);
break;
}
}
public bool IsReusable
{
get
{
return false;
}
}
}