最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

asp.net繼承IHttpHandler接口實(shí)現(xiàn)給網(wǎng)站圖片添加水印功能實(shí)例

 更新時(shí)間:2016年07月26日 09:21:53   作者:ajunfly  
這篇文章主要介紹了asp.net繼承IHttpHandler接口實(shí)現(xiàn)給網(wǎng)站圖片添加水印功能,實(shí)例分析了asp.net基于IHttpHandler接口實(shí)現(xiàn)網(wǎng)站圖片水印功能的具體步驟與相關(guān)技巧,需要的朋友可以參考下

本文實(shí)例講述了asp.net繼承IHttpHandler接口實(shí)現(xiàn)給網(wǎng)站圖片添加水印功能。分享給大家供大家參考,具體如下:

先展示圖片效果:

1. 在App_Code下添加類文件,命名為ImageSY 文件內(nèi)容如下

public class ImageSY : IHttpHandler
{
  public ImageSY()
  {
    //
    //TODO: 在此處添加構(gòu)造函數(shù)邏輯
    //
  }
  #region IHttpHandler 成員
  public bool IsReusable
  {
    get { return true; }
  }
  public void ProcessRequest(HttpContext context)
  {
    //獲得請(qǐng)求的物理圖片路徑
    string imagePath = context.Request.PhysicalPath;
    System.Drawing.Image image = null;
    if (File.Exists(imagePath))
    {
      //定義水印文字
      string text = "本圖片來至我的網(wǎng)站";
      //定義水印文字字體大小
      int fontSize = 22;
      //水印文字字體
      Font font = new Font("宋體", fontSize);
      //根據(jù)圖片物理地址加載圖片
      image = System.Drawing.Image.FromFile(imagePath);
      Graphics g = Graphics.FromImage(image);
      //獲取要繪制水印文字所需要的顯示區(qū)域大小
      SizeF size = g.MeasureString(text, font);
      if (size.Width > image.Width || size.Height > image.Height)
      {
      }
      else
      {
        Brush brush = Brushes.Red;
        g.DrawString(text, font, brush, image.Width - size.Width, image.Height - size.Height);
        g.Dispose();
      }
    }
    else
    {
    }
    image.Save(context.Response.OutputStream, ImageFormat.Jpeg);
  }
  #endregion
}

2. 配置WebConfig,添加Location新節(jié)點(diǎn)

<location path="images">
 <system.web>
  <httpHandlers>
  <!---對(duì)jpg文件添加水印-->
  <add verb="*" type="ImageSY" path="*.jpg"/>
  <add verb="*" type="ImageSY" path="*.gif"/>
  <add verb="*" type="ImageSY" path="*.bmp"/>
  </httpHandlers>
 </system.web>
 </location>

3. 測(cè)試,新建aspx頁面,顯示圖片,水印就會(huì)自動(dòng)加上了

更多關(guān)于asp.net相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《asp.net操作json技巧總結(jié)》、《asp.net字符串操作技巧匯總》、《asp.net操作XML技巧總結(jié)》、《asp.net文件操作技巧匯總》、《asp.net ajax技巧總結(jié)專題》及《asp.net緩存操作技巧總結(jié)》。

希望本文所述對(duì)大家asp.net程序設(shè)計(jì)有所幫助。

相關(guān)文章

最新評(píng)論

湄潭县| 南涧| 安吉县| 武平县| 和顺县| 宜良县| 梅河口市| 蒙自县| 阳曲县| 奉贤区| 耒阳市| 林甸县| 泰州市| 新河县| 怀仁县| 南京市| 镇原县| 二手房| 越西县| 阳西县| 洪雅县| 霞浦县| 高雄市| 通许县| 太白县| 安宁市| 庐江县| 两当县| 丹江口市| 通化县| 宣威市| 九江县| 威信县| 竹溪县| 紫阳县| 吴江市| 康马县| 安新县| 宜章县| 得荣县| 北川|