• IHttpHandler给图片加水印


        /// <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;
                }
            }
        }
  • 相关阅读:
    Noip2015总结
    BZOJ2457 BeiJing2011 双端队列
    Noip模拟考第三题——饥饿游戏
    HDU 2196 求树上所有点能到达的最远距离
    O(V*n)的多重背包问题
    Noip2008双栈排序
    USACO 4.1.2 栅栏的木料
    字符串专题
    网络流24题刷题记录
    解模线性方程组 非互质中国剩余定理
  • 原文地址:https://www.cnblogs.com/caoyc/p/6222132.html
Copyright © 2020-2023  润新知