日期:2014-05-16  浏览次数:20458 次

继承IHttpHandler实现全局图片水印
本帖最后由 butterfly_onfly 于 2014-03-10 23:29:51 编辑
创建了一个
ImageHandler类库,创建了个namespace ImageHandler
{
    public class WriteHandler:IHttpHandler
    {
        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
        public void ProcessRequest(HttpContext context)
        {
          
            //获取图片路径
            string imgpath = context.Request.PhysicalPath;
            //获取水印图片路径
            string wpath = context.Server.MapPath("~/img/logo.jpg");
            //获取默认图片路径
            string dpath = context.Server.MapPath("~/img/noPicture.jpg");
            Image img;
            if (File.Exists(imgpath))//判断图片是否存在
            {
                img = Image.FromFile(imgpath);//加载图片
                Image wimg = Image.FromFile(wpath);//加载水印图片
                Graphics g = Graphics.FromImage(img);//让显示的图片设置为背景图片
                //绘制显示图片的矩形
                Rectangle re = new Rectangle(img.Width - wimg.Width, img.Height - wimg.Height, wimg.Width, wimg.Height);
                //绘制水印图片的矩形
                Rectangle re1 = new Rectangle(0, 0, wimg.Width, wimg.Height);
                g.DrawImage(wimg, re, re1, GraphicsUnit.Pixel);
                //文字水印
                //LinearGradientBrush brush = new LinearGradientBrush(new Rectangle(0, 0, wimg.Width, wimg.Height), Color.White, Color.WhiteSmoke, 1.3f, true);
         &