前台
<asp:Image ID="Image1" ImageUrl="~/SmallPic.ashx?pic=20091012151224586.jpg" runat="server" />
//一般处理文件ashx
<%@ WebHandler Language="C#" Class="SmallPic" %>
using System;
using System.Web;
using System.IO;
using System.Drawing;
using System.Drawing.Imaging;
public class SmallPic : IHttpHandler {
public void ProcessRequest (HttpContext context) {
//取得传进来的文件路径
string picFilePath = context.Request.QueryString["pic"].Trim();
context.Response.ContentType = "Image/JPEG";
context.Response.Clear();
context.Response.BufferOutput = true;
using (MemoryStream ms = new MemoryStream(System.IO.File.ReadAllBytes(context.Server.MapPath(picFilePath))))
{
using (Bitmap bmp=new Bitmap(ms))
{
using (Bitmap smallbmp = new Bitmap(bmp, Convert.ToInt32(bmp.Width * 0.15), Convert.ToInt32(bmp.Height * 0.15)))
{
smallbmp.Save(context.Response.OutputStream, ImageFormat.Jpeg);
}
}
}
}
public bool IsReusable {
get {
return false;
}
}
}