欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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 = "本圖片來(lái)至我的網(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頁(yè)面,顯示圖片,水印就會(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)論