using
System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Drawing;
using System.IO;
namespace
BookShop.Web
{
p
ublic class WaterMark : IHttpHandler
{
PRivate const string WATERMARK_URL = "~/Images/watermark.
jpg"; //水印图片
private const string DEFAULTIMAGE_URL = "~/Images/default.jpg";<span style="wh
ITe
-space:pre"> </span> //默认图片
#region IHttpHandler 成员
public bool IsReusable
{
get { return false; }
}
public void ProcessRequest(HttpContext context)
{
//context.Request.Phys
icalPath //获得用户请求的文件物理路径
System.Drawing.Image Cover;
//判断请求的物理路径中,
是否存在文件
if (File.Exists(context.Request.PhysicalPath))
{
//加载文件
Cover = Image.
FromFile(context.Request.PhysicalPath);
//加载水印图片
Image watermark = Image.F
romFile(context.Request.MapPath(WATERMARK_URL));
//通过书的封面得到绘图对像
Graphics g = Graphics.FromImage(Cover);
//在image上
绘制水印
g.DrawImage(watermark, new Rectangle(Cover.Width - watermark.Width, Cover.H
eight - 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(DEFAULTIMAGE_URL));
}
//设置
输出格式 context.Response.ContentTy
PE = "image/jpeg";
//将图片存入输出流
Cover.Save(context.Response.OutputStre
am, System.Drawing.Ima
ging.ImageFormat.Jpeg);
Cover.Dispose();
context.Response.End();
}
#endregion
}
}