ASP.NET動態添加浮水印類

來源:互聯網
上載者:User

WaterMarke.cs 類 code

 

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
/// <summary>
/// WaterMarker 的摘要說明
/// </summary>
public class WaterMarker : IHttpHandler
{
    public WaterMarker()
    {
        //
        // TODO: 在此處添加建構函式邏輯
        //
    }
    public bool IsReusable
    {
        get { return true; }
    }
    public void ProcessRequest(HttpContext context)
    {
        //擷取請求的實體路徑
        string imagePath = context.Request.PhysicalPath;
        System.Drawing.Image image = null;
        //判斷指定的實體路徑是否存在
        if (File.Exists(imagePath))
        {
            //定義浮水印文字
            string text = "本圖片來自夏楚楓的部落格";
            //定義浮水印文字字型的大小
            int fontSize = 12;
            //浮水印文字字型
            Font font = new Font("宋體", fontSize);
            //根據圖片的實體路徑載入圖片
            image = System.Drawing.Image.FromFile(imagePath);
            Graphics g = Graphics.FromImage(image);
            //擷取要繪製浮水印文字所需要地區的大小
            SizeF size = g.MeasureString(text, font);
            if (size.Width > image.Width || size.Height > image.Height)
            {
                //如果要顯示圖片的尺寸都不足以顯示按照指定文字字型來添加浮水印
                //可以減小的字型的大小或者不添加浮水印(太小了沒辦法添加嘛)
            }
            else
            {
                //添加浮水印文字
                Brush br = Brushes.Red;
                g.DrawString(text, font, br, image.Width - size.Width, image.Height - size.Height);
                g.Dispose();
            }

        }
        else //如果不存在指定一個預設的圖片進行顯示
        {
            imagePath = context.Server.MapPath("~/image/default.jpg");
            image = System.Drawing.Image.FromFile(imagePath);
        }
        image.Save(context.Response.OutputStream, ImageFormat.Jpeg);//將添加浮水印的圖片輸出

    }
}

 

webconfig 中的配置 

方法一:在所在檔案夾添加一個webconfig檔案,然後再在
 <system.web>節點中添加下面的代碼

<httpHandlers>
    <!--對jpg檔案添加浮水印-->
    <add path="*" verb="*.jpg" type="WaterMarker"/>
    <!--對bmp檔案添加浮水印-->
    <add verb="*" path="*.bmp" type="WaterMarker"/>
   </httpHandlers>

 

方法二: 不在圖片所在的檔案夾下添加webconfig 而是直接在根目錄下的webconfig中的配置

不用自動產生的那個<System.web>中配置 而是在<configuration>節點中添加如下代碼

 <location path="image">
  <system.web>
   <httpHandlers>
    <!--對jpg檔案添加浮水印-->
    <add path="*" verb="*.jpg" type="WaterMarker"/>
    <!--對bmp檔案添加浮水印-->
    <add verb="*" path="*.bmp" type="WaterMarker"/>
   </httpHandlers> 
  </system.web> 
 </location>

 

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.