Asp.Net圖片數字水印的實現

笨笨鼠→_→發表於2012-07-06

第一種實現方法:  ---  利用IHttpHandler

    1. 新增新建項 --  一般處理程式
    2. <%@ WebHandler Language="C#" Class="JPGHandler" %>
      using System;
      using System.Web;
      using System.Drawing;
      public class JPGHandler : IHttpHandler {
         
          public void ProcessRequest (HttpContext context) {
             
             
              // ①載入將要水印的圖片
              Image img = Image.FromFile(context.Server.MapPath("~/img/BookCovers/9293450.jpg"));
             
              // ②載入水印圖片
              Image watermark = Image.FromFile(context.Request.MapPath("~/img/WaterMark.jpg"));
             
              // ③例項化畫布
              Graphics g = Graphics.FromImage(img);
             
              // ④在image上繪製水印
              g.DrawImage(watermark, new Rectangle(img.Width - watermark.Width, img.Height - watermark.Height, watermark.Width, watermark.Height));
             
              // ⑤釋放畫布
              g.Dispose();
             
              // ⑥釋放水印圖片
              watermark.Dispose();
             
              // ⑦設定輸出型別為JPEG圖片
              context.Response.ContentType = "image/jpeg";
             
              // ⑧將修改的圖片存入輸出流
              img.Save(context.Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg);
             
              // ⑨釋放圖片
              img.Dispose();
             
              // 最後: 清空快取,傳送Response
              context.Response.End();
          }
       
          public bool IsReusable {
              get {
                  return true;
              }
          }
      }

第二種實現方法: --  全域性Handler方式

    1. 修改配置檔案
      <httpHandlers>
               <add verb="*" path="img/BookCovers/*.jpg" type="ImgHandler"/>
      </httpHandlers>
    2. 編寫handler類
      using System;
      using System.Collections.Generic;
      using System.Linq;
      using System.Web;
      using System.IO;
      using System.Drawing;
      /// <summary>
      ///ImgHandler 的摘要說明
      /// </summary>
      public class ImgHandler:IHttpHandler
      {
          public ImgHandler()
          {
              //
              // TODO: 在此處新增建構函式邏輯
              //
          }

          #region IHttpHandler 成員
          public bool IsReusable
          {
              get { return false; }
          }

          private const string WATER_URL = "~/img/WaterMark.jpg";
          private const string DEFAULT_URL = "~/img/default.jpg";
          

          public void ProcessRequest(HttpContext context)
          {
              Image img = null;
              if (File.Exists(context.Request.PhysicalPath))
              {
                  img = Image.FromFile(context.Request.PhysicalPath);
                  Image water = Image.FromFile(context.Request.MapPath(WATER_URL));
                  Graphics g = Graphics.FromImage(img);
                  g.DrawImage(water, new Rectangle(img.Width - water.Width, img.Height - water.Height, water.Width, water.Height));
                  g.Dispose();
                  water.Dispose();
              }
              else
              {
                  img = Image.FromFile(context.Request.MapPath(DEFAULT_URL));
              }
              context.Response.ContentType = "image/jpeg";
              img.Save(context.Response.OutputStream,System.Drawing.Imaging.ImageFormat.Jpeg);
              img.Dispose();
              context.Response.End();
          }
          #endregion
      }

相關文章