zoukankan      html  css  js  c++  java
  • 图片水印

     1 public class WaterImgHandler:IHttpHandler
     2     {
     3 
     4         public bool IsReusable
     5         {
     6             get { return false; }
     7         }
     8 
     9         public void ProcessRequest(HttpContext context)
    10         {
    11                  // 指定水印图片
    12             string water_Url = "~/wImages/logo.gif";
    13             // 指定默认图片
    14             string default_Url = "~/wImages/error.jpg";
    15             // 创建图片对象,用于存放用户读取的图片
    16             System.Drawing.Image g;
    17             // 判断所请求的图片是否存在,存在就给图片加水印
    18             if (File.Exists(context.Request.PhysicalPath))
    19             {
    20                 g = System.Drawing.Image.FromFile(context.Request.PhysicalPath);
    21                 // 加载水印图片
    22                 System.Drawing.Image waterImg = System.Drawing.Image.FromFile(context.Request.MapPath(water_Url));
    23                 Graphics gh = Graphics.FromImage(g);
    24                 gh.DrawImage(waterImg, new RectangleF(0, g.Height - waterImg.Height, 100, 20));
    25 
    26                 // 加载水印字符串
    27                 Font font = new System.Drawing.Font("Arial", 6, System.Drawing.FontStyle.Bold);
    28                 gh.DrawString("abcdefg", font, Brushes.White, 2, 2);
    29                 gh.Dispose();
    30                 waterImg.Dispose();
    31             }
    32             else { 
    33                 // 图片不存在,显示默认图片
    34                 g = System.Drawing.Image.FromFile(context.Request.MapPath(default_Url));
    35             }
    36             context.Request.ContentType = "image/jpeg";
    37             g.Save(context.Response.OutputStream,System.Drawing.Imaging.ImageFormat.Jpeg);
    38             g.Dispose();
    39             context.Response.End();
    40 
    41         }
    42     }
    43         // 最后在配置文件Web.config中写
    44       <httpHandlers>
    45          <add verb="*" path="*.jpg" type="Picture.WaterImgHandler"/>
    46      </httpHandlers>
  • 相关阅读:
    linux mint系统 cinnamon桌面 发大镜功能
    解决sublime 的 package control 问题here are no packages available for installation
    python Tkinter 的 Text 保持焦点在行尾
    pyhton 下 使用getch(), 输入字符无需回车
    python3.5 安装twisted
    SPOJ bsubstr
    AVL的删除写法的一个错误
    病毒侵袭持续中
    POJ2778 DNA sequence
    HDU3068 最长回文串
  • 原文地址:https://www.cnblogs.com/xjx8205/p/4681241.html
Copyright © 2011-2022 走看看