asp.net、C#动态添加图片水印

 

例子1:

web.config

<httpHandlers>  
            <remove verb="*" path="*.asmx"/>  
            <add verb="*" path="*.asmx" validate="false" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>  
            <add verb="*" path="*_AppService.axd" validate="false" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>  
            <add verb="GET,HEAD" path="ScriptResource.axd" type="System.Web.Handlers.ScriptResourceHandler, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" validate="false"/>  
            <add verb="*" path="images/shangjia/*/*.jpg"  type="CoverHandler"/>  
            <add verb="*" path="images/shangjia/*/*/*.jpg"  type="CoverHandler"/>  
            <add verb="*" path="admin/TotalUpImg/VedioImg/*.jpg" type="CoverHandler1"/>  
        </httpHandlers>  
<httpHandlers>  
 
            <remove verb="*" path="*.asmx"/>  
            <add verb="*" path="*.asmx" validate="false"   
type="System.Web.Script.Services.ScriptHandlerFactory,   
System.Web.Extensions, Version=1.0.61025.0, Culture=neutral,   
PublicKeyToken=31bf3856ad364e35"/>  
            <add verb="*" path="*_AppService.axd" validate="false"   
type="System.Web.Script.Services.ScriptHandlerFactory,   
System.Web.Extensions, Version=1.0.61025.0, Culture=neutral,   
PublicKeyToken=31bf3856ad364e35"/>  
            <add verb="GET,HEAD" path="ScriptResource.axd"   
type="System.Web.Handlers.ScriptResourceHandler, System.Web.Extensions,   
Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"   
validate="false"/>  
            <add verb="*" path="images/shangjia/*/*.jpg"    
type="CoverHandler"/>  
            <add verb="*" path="images/shangjia/*/*/*.jpg"    
type="CoverHandler"/>  
            <add verb="*" path="admin/TotalUpImg/VedioImg/*.jpg"   
type="CoverHandler1"/>  
        </httpHandlers>

 

 

CoverHandler.cs

 

using System;  
using System.Data;  
using System.Configuration;  
using System.Web;  
using System.IO;  
using System.Drawing;  
using System.Drawing.Imaging;  
  
/// <summary>  
/// CoverHandler 的摘要说明  
/// </summary>  
public class CoverHandler : IHttpHandler  
{  
    public CoverHandler()  
    {  
        //  
        // TODO: 在此处添加构造函数逻辑  
        //  
    }  
    private const string WATERMARK_URL = "~/images/WaterMark.png";  
    private const string DEFAULTIMAGE_URL = "~/images/default.jpg";  
    #region IHttpHandler 成员  
  
  
    public void ProcessRequest(HttpContext context)  
    {  
        System.Drawing.Image Cover;  
        if (File.Exists(context.Request.PhysicalPath))  
        {  
            Cover = Image.FromFile(context.Request.PhysicalPath);  
            Image watermark = Image.FromFile(context.Request.MapPath(WATERMARK_URL));  
            Graphics g = Graphics.FromImage(Cover);  
            Random r = new Random();  
            int width = r.Next(10,Cover.Width - watermark.Width);  
            int height = r.Next(10,Cover.Height - watermark.Height);  
            g.DrawImage(watermark, new Rectangle(width, height, watermark.Width, watermark.Height), 0, 0, watermark.Width, watermark.Height, GraphicsUnit.Pixel);  
            g.Dispose();  
            watermark.Dispose();  
        }  
        else  
        {  
            Cover = Image.FromFile(context.Request.MapPath(DEFAULTIMAGE_URL));  
        }  
        context.Response.ContentType = "image/Jpeg";  
        Cover.Save(context.Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg);  
        Cover.Dispose();  
        context.Response.End();  
  
    }  
    public bool IsReusable  
    {  
        get  
        {  
            return false;  
        }  
    }  
 
    #endregion  

 

 

 
 

例子2:

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 //如果不存在指定一个默认的图片进行显示http://www.xuehai.net
        {
            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>

你可能感兴趣的:(asp.net)