自定义HTTPHandler实现数字水印效果

<!--

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;
}

}


}

通过实现IHttpHandler接口,自定义实现HTTP请求的处理

希望对于想要了解ASP.NET内部实现机制的朋友给予帮助

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值