zoukankan      html  css  js  c++  java
  • 在图片上加水印或写字

    使用一般处理程序在已有图片上加水印或者写字

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    public void ProcessRequest(HttpContext context)
            {
                context.Response.ContentType = "text/plain";
                //原图
                System.Drawing.Bitmap bitmap = new Bitmap(context.Server.MapPath("Uploads/201307269946.jpg"));
                //水印图
                System.Drawing.Bitmap bitmap1 = new Bitmap(context.Server.MapPath("Uploads/201307269390.png"));
      
                Graphics g = Graphics.FromImage(bitmap);
      
                //Drawing String
                //g.DrawString("www.cnblogs.com/g1mist", new Font("黑体", 30), Brushes.Red, new PointF(10, 60));
      
                //Drawing Images
                //将水印图画在原图的(10,60)位置上
                g.DrawImage(bitmap1, new Point(10, 60));
      
                bitmap.Save(context.Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg);
      
            }

      但是,一个一般处理程序一次在一张图片上加水印,如果需要在每一个对图片的请求中都对图片加上水印,那就要使用全局处理程序,新建一个类"WaterMarkHelper",并且实现IHttpHandler接口

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    public class WaterMarkHelper : IHttpHandler
        {
            public void ProcessRequest(HttpContext context)
            {
                //原始请求路径
                string rawurl = context.Request.RawUrl;
                //取到物理路径
                string path = context.Server.MapPath(rawurl);
                //取到水印的路径
                string logoPath = context.Server.MapPath("201307269390.png");
      
                //原图
                using (Image img = Image.FromFile(path))
                {
                    //水印图
                    using (Image logoImage = Image.FromFile(logoPath))
                    {
                        //原图为画板
                        using (Graphics g = Graphics.FromImage(img))
                        {
                            //在原图上画上水印
                            g.DrawImage(logoImage, 10, 60, logoImage.Width, logoImage.Height);
      
                            //保存图片
                            img.Save(context.Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg);
                        }
                    }
                }
            }
      
            public bool IsReusable
            {
                get { return false; }
            }
        }

      接着在Web.config中进行配置:

      .net 4.5:

      

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    <configuration>
      <system.web>
        <compilation debug="true" targetFramework="4.5" />
        <httpRuntime targetFramework="4.5" />
      </system.web>
      <system.webServer>
        <handlers>
          <add name="water" verb="*" path="Uploads/*.jpg" type="FileUpload.WaterMarkHelper"/>
        </handlers>
      </system.webServer>
    </configuration>

      .net 4.0

      

    1
    2
    3
    4
    5
    6
    7
    8
    9
    <configuration>
      <system.web>
        <compilation debug="true" targetFramework="4.5" />
        <httpRuntime targetFramework="4.5" />
        <httpHandlers>
          <add verb="*" path="Uploads/*.jpg" type="FileUpload.WaterMarkHelper"/>
        </httpHandlers>
      </system.web>
    </configuration>

      type: "命名空间.类名,程序集名称",如果没有命名空间就只写类名。和网站在同一个程序集就不用写程序及名称

  • 相关阅读:
    五、drf路由组件
    四、drf视图组件
    三、drf请求&响应
    二、drf序列化器
    解决SQL Server管理器无法连接远程数据库的问题
    常见网络摄像机(摄像头)的端口及RTSP地址
    海康、大华网络摄像机RTSP URL格式组成及参数配置
    SQL 查询某字段不为空
    SqlServer中保留几位小数的两种做法
    sql重复数据只取一条记录
  • 原文地址:https://www.cnblogs.com/jameslif/p/3227446.html
Copyright © 2011-2022 走看看