创建一个Generic Handler
(即 ashx 文件)在您的 ASP.NET 网站或应用程序中,并将以下代码放入其中。例如将其称为“Handler.ashx”。
现在,在浏览器中使用:Handler.ashx?x=100&y=300&w=800&h=500&scalew=160&scaleh=100
Code of 处理程序.ashx file:
<%@ WebHandler Language="C#" Class="Handler" %>
using System;
using System.Web;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
public class Handler : IHttpHandler {
public void ProcessRequest (HttpContext context) {
context.Response.ContentType = "image/jpeg";
int x = int.Parse(context.Request["x"]);
int y = int.Parse(context.Request["y"]);
int h = int.Parse(context.Request["h"]);
int w = int.Parse(context.Request["w"]);
int scalew = int.Parse(context.Request["scalew"]);
int scaleh = int.Parse(context.Request["scaleh"]);
using (Image img = CustomCrop(w, h, x, y, scalew, scaleh))
img.Save(context.Response.OutputStream,ImageFormat.Jpeg);
}
public static Image CustomCrop(int width, int height, int x, int y, int scalwidth, int scalheight)
{
try
{
Image image = Image.FromFile("c:\\x.jpg");
Bitmap bmp = new Bitmap(scalwidth, scalheight, PixelFormat.Format24bppRgb);
bmp.SetResolution(80, 60);
Graphics gfx = Graphics.FromImage(bmp);
gfx.SmoothingMode = SmoothingMode.AntiAlias;
gfx.InterpolationMode = InterpolationMode.HighQualityBicubic;
gfx.PixelOffsetMode = PixelOffsetMode.HighQuality;
gfx.DrawImage(image, new Rectangle(0, 0, scalwidth, scalheight), x, y, width, height, GraphicsUnit.Pixel);
return bmp;
}
catch (Exception ex)
{
//MessageBox.Show(ex.Message);
return null;
}
}
public bool IsReusable {
get {
return false;
}
}
}
EDIT:
关于通用处理程序的一些参考:
HTTP 处理程序和 HTTP 模块概述 http://msdn.microsoft.com/en-us/library/bb398986.aspx
@Web处理程序 http://msdn.microsoft.com/en-us/library/ms366713.aspx- ashx 文件如何工作。