<!--
Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/
-->
using
System;
using
System.Web;
using
System.Drawing;
using
System.Drawing.Imaging;
using
System.IO;


/**/
///<summary>
///映射文件后缀名方式的数字水印
///</summary>
public
class
CoverHandler:IHttpHandler

{
//数字水印路径
privateconststringWATERMARK_URL="~/Images/WaterMark.jpg";
//默认图片路径
privateconststringDEFAULT_PIC="~/Images/default.jpg";


publicvoidProcessRequest(HttpContextcontext)


{
ImageCover;
//判断请求的物理路径中,是否存在该文件
if(File.Exists(context.Request.PhysicalPath))


{
//加载文件
Cover=Image.FromFile(context.Request.PhysicalPath);
//加载水印图片
Imagewatermark=Image.FromFile(context.Request.MapPath(WATERMARK_URL));
//实例化画布
Graphicsg=Graphics.FromImage(Cover);
//Cover上绘制水印
g.DrawImage(watermark,newRectangle(Cover.Width-watermark.Width,Cover.Height-watermark.Height,watermark.Width,watermark.Height),0,0,watermark.Width,watermark.Height,GraphicsUnit.Pixel);
//释放画布
g.Dispose();
//释放水印图片
watermark.Dispose();
}
else


{
//加载默认图片
Cover=Image.FromFile(context.Request.MapPath(DEFAULT_PIC));
}
//设置输出格式
context.Response.ContentType="image/jpeg";
//将图片存入输出流
Cover.Save(context.Response.OutputStream,ImageFormat.Jpeg);
//释放资源
Cover.Dispose();
//停止HTTP响应
context.Response.End();
}

publicboolIsReusable


{
get


{
returnfalse;
}
}

}
Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/
-->










































































通过实现IHttpHandler接口,自定义实现HTTP请求的处理
希望对于想要了解ASP.NET内部实现机制的朋友给予帮助