使用ashx显示小图片(不存在的小图)
ashx文件与.aspx文件类似,可以通过它来调用HttpHandler类,它免去了普通.aspx页面的控件解析以及页面处理的过程。.ashx文件适合产生供浏览器处理的、不需要回发处理的数据格式,例如用于生成动态图片、动态文本等内容。
<%@ WebHandler Language="C#" Class="showpic" %>
using System;
using System.Web;
using System.Drawing;
using System.IO;
public class showpic : IHttpHandler {
public void ProcessRequest (HttpContext context) {
string pic = context.Request["pic"];
Bitmap bit = new Bitmap(context.Server.MapPath("~/tp/image_cp/" + pic));
//context.Server.MapPath("~/tp/image_cp/" + pic)是图片路径哦
//bit.Height
MemoryStream ms=new MemoryStream();
int newh=0;
int neww=0;
//
if (bit.Width < 150 && bit.Height < 150)
{
newh = bit.Height;
neww = bit.Width;
}
else
{
double pwidth = (double)bit.Width / 150;
double pheight = (double)bit.Height / 150;
if (pwidth > pheight)
{
neww = 150;
newh = Convert.ToInt32(bit.Height / pwidth);
}
else
{
newh = 150;
neww = Convert.ToInt32(bit.Width / pheight);
}
}
//生成小图
Bitmap newbit = new Bitmap(bit, neww, newh);
newbit.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
context.Response.ContentType = "image/jpeg";
context.Response.StatusCode = 200;
context.Response.BinaryWrite(ms.ToArray());
//context.Response.BinaryWrite(
}
public bool IsReusable {
get {
return false;
}
}
}