/// <summary>
    /// WaterMarkHandlher 的摘要说明
    /// </summary>
    public class WaterMarkHandlher : IHttpHandler
    {
        static string waterPath = "~/image/watermark.png"; //水印图片路径
        static string defaultPath = "~/image/default.jpg"; //默认图片路径

        public void ProcessRequest(HttpContext context)
        {
            string coverPath = context.Server.MapPath(context.Request.Path);
            Image coverImage;
            //如果文件不存在则加载默认图片
            if (!File.Exists(coverPath))
            {
                coverImage = Image.FromFile(context.Server.MapPath(defaultPath));
            }
            //图片存在
            else
            {
                //加载图片
                coverImage = Image.FromFile(coverPath);
                //加载水印
                Image water = Image.FromFile(context.Server.MapPath(waterPath));
                //实例化画布
                Graphics g = Graphics.FromImage(coverImage);
                //绘制水印
                g.DrawImage(water,
                    new Rectangle(coverImage.Width - water.Width, coverImage.Height - water.Height, water.Width,
                        water.Height), 0, 0, water.Width, water.Height, GraphicsUnit.Pixel);
                //释放画布
                g.Dispose();
                //释放水印
                water.Dispose();
            }

            context.Response.ContentType = "image/jpeg";
            coverImage.Save(context.Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg);
            coverImage.Dispose();
            context.Response.End();
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }

 

 posted on 2016-12-26 14:12  Just_Do  阅读(338)  评论(0编辑  收藏  举报