zoukankan      html  css  js  c++  java
  • 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;
                }
            }
        }
  • 相关阅读:
    好多天没写了,郁闷
    昨天很受教育
    恼火的服务器
    欢迎访问我的博客网站
    体育产品论坛
    参考书目
    web2.0与数字标准
    用户产生内容与网站做内容
    Using x++ code reading data from csv file format
    Find out specified the folder for all the files
  • 原文地址:https://www.cnblogs.com/caoyc/p/6222132.html
Copyright © 2011-2022 走看看