现在的位置: 首页 > 综合 > 正文

给图片添加水印效果图的函数(可以在图片上添加自己的版权和LOGO图片的水印)

2012年08月07日 ⁄ 综合 ⁄ 共 6885字 ⁄ 字号 评论关闭

前阵子在codeproject上找到一篇添加水印的文章,保存了下来,一直没时间看,现在闲下来了,整理成了一个函数
LOGO图:

原图:

效果图:

/*
 * 给图片设置水印效果图的函数
 * 可以在图片上添加自己的版权和LOGO图片的水印
 * 
 * 原作者:Joel Neubeck 
 * 
 * 本人在原作者的基础上进行了部分修改,并改为一个单独的函数
 * 大家可以根据需要修改此代码
 * 但请保留原作者信息.
 * 
 * 星宿.net(zhuhee)
 * 修改于2006-6-14
 * 
 
*/

using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.Drawing.Drawing2D;

namespace WebWaterMark.Components
{
    
/// <summary>
    
/// watermark 的摘要说明。
    
/// </summary>

    public class watermark
    
{
        
public watermark()
        
{
            
//
            
// TODO: 在此处添加构造函数逻辑
            
//
        }

        
/// <summary>
        
/// 用于处理图片
        
/// 给图片加入水印
        
/// </summary>
        
/// <param name="Copyright">需要写入的版权信息</param>
        
/// <param name="MarkBmpPath">需要假如的logo图片</param>
        
/// <param name="photoPath">需要处理的图片的路径</param>
        
/// <param name="savePhotoPath">保存的图片路径</param>

        public void MakeWatermark(string Copyright,string MarkBmpPath,string photoPath,string savePhotoPath)
        
{

            
//创建一个image对象,即要被处理的图片
            Image imgPhoto = Image.FromFile(photoPath);
            
int phWidth = imgPhoto.Width;
            
int phHeight = imgPhoto.Height;

            
//创建原始图片大小的Bitmap
            Bitmap bmPhoto = new Bitmap(phWidth, phHeight, PixelFormat.Format24bppRgb);

            bmPhoto.SetResolution(imgPhoto.HorizontalResolution, imgPhoto.VerticalResolution);

            
//将位图bmPhoto加载到Graphics对象
            Graphics grPhoto = Graphics.FromImage(bmPhoto);

            
//创建一个需要填充水银的Image对象
            Image imgWatermark = new Bitmap(MarkBmpPath);
            
int wmWidth = imgWatermark.Width;
            
int wmHeight = imgWatermark.Height;

            
//------------------------------------------------------------
            
//第一步,插入版权信息
            
//------------------------------------------------------------

            
//设置图象的成相质量
            grPhoto.SmoothingMode = SmoothingMode.AntiAlias;

            
//将原始图象绘制到grPhoto上
            grPhoto.DrawImage(
                imgPhoto,                               
// 要绘制的Image对象
                new Rectangle(00, phWidth, phHeight), // 绘制图象的位置和大小
                0,                                      // 要绘制的原图象部分的左上角的X坐标
                0,                                      // 要绘制的原图象部分的左上角的Y坐标
                phWidth,                                // 要绘制的原图象的高度
                phHeight,                               // 要绘制的原图象的宽度
                GraphicsUnit.Pixel);                    // 源矩形的度量单位

            
//-------------------------------------------------------
            
//字体大小放在一个数组中,最大字体为32.
            
//感觉用处不大,可以自己再修改这里
            
//-------------------------------------------------------
            int[] sizes = new int[]{8,7,6,5,4};

            Font crFont 
= null;
            SizeF crSize 
= new SizeF();

            
//循环测试数组中所定义的字体大小是否适合版权信息,如果合适就使用此种字体大小
            for (int i=0 ;i<5; i++)
            
{
                
//设置字体类型,可以单独提出,作为参数
                crFont = new Font("宋体", sizes[i], FontStyle.Bold);
                
//测量此种字体大小
                crSize = grPhoto.MeasureString(Copyright, crFont);

                
if((ushort)crSize.Width < (ushort)phWidth)
                    
break;
            }


            
//给底部保留%3的空间
            int yPixlesFromBottom = (int)(phHeight *.03);

            
//设置字体在图片中的位置
            float yPosFromBottom = ((phHeight - yPixlesFromBottom)-(crSize.Height/2));

            
//float xCenterOfImg = (phWidth/2);
            float xCenterOfImg = (phWidth-(crSize.Width)/2);
            
//设置字体居中
            StringFormat StrFormat = new StringFormat();
            StrFormat.Alignment 
= StringAlignment.Center;
            
            
//设置绘制文本的颜色和纹理 (Alpha=153)
            SolidBrush semiTransBrush2 = new SolidBrush(Color.FromArgb(153000));

            
//将版权信息绘制到图象上
            grPhoto.DrawString(Copyright,                     //版权信息
                crFont,                                       //字体
                semiTransBrush2,                              //绘制文本的颜色及纹理
                new PointF(xCenterOfImg+1,yPosFromBottom+1),  //绘制文本的位置
                StrFormat);                                   //格式

            
//设置绘制文本的颜色和纹理 (Alpha=153)
            SolidBrush semiTransBrush = new SolidBrush(Color.FromArgb(153255255255));

            
//重新绘制版权信息,以让其具有阴影效果
            
//将文本向又下移动一个象素
            grPhoto.DrawString(Copyright,                 //版权信息
                crFont,                                   //字体
                semiTransBrush,                           //绘制文本的颜色及纹理
                new PointF(xCenterOfImg,yPosFromBottom),  //绘制文本的位置
                StrFormat);                               //格式

            

            
//------------------------------------------------------------
            
//第二步,插入图片水印
            
//------------------------------------------------------------

            
//在原来修改过的bmPhoto上创建一个水银位图
            Bitmap bmWatermark = new Bitmap(bmPhoto);

            bmWatermark.SetResolution(imgPhoto.HorizontalResolution, imgPhoto.VerticalResolution);
            
//将位图bmWatermark加载到Graphics对象
            Graphics grWatermark = Graphics.FromImage(bmWatermark);

            
//To achieve a transulcent watermark we will apply (2) color 
            
//manipulations by defineing a ImageAttributes object and 
            
//seting (2) of its properties.
            ImageAttributes imageAttributes = new ImageAttributes();

            
//The first step in manipulating the watermark image is to replace 
            
//the background color with one that is trasparent (Alpha=0, R=0, G=0, B=0)
            
//to do this we will use a Colormap and use this to define a RemapTable
            ColorMap colorMap = new ColorMap();

            
//My watermark was defined with a background of 100% Green this will
            
//be the color we search for and replace with transparency
            colorMap.OldColor = Color.FromArgb(25502550);
            colorMap.NewColor 
= Color.FromArgb(0000); 

            ColorMap[] remapTable 
= {colorMap};

            imageAttributes.SetRemapTable(remapTable, ColorAdjustType.Bitmap);

            
//The second color manipulation is used to change the opacity of the 
            
//watermark.  This is done by applying a 5x5 matrix that contains the 
            
//coordinates for the RGBA space.  By setting the 3rd row and 3rd column 
            
//to 0.3f we achive a level of opacity
            float[][] colorMatrixElements = 
                                                
new float[] {1.0f,  0.0f,  0.0f,  0.0f0.0f},       
                                                
new float[] {0.0f,  1.0f,  0.0f,  0.0f0.0f},        
                                                
new float[] {0.0f,  0.0f,  1.0f,  0.0f0.0f},        
                                                
new float[] {0.0f,  0.0f,  0.0f,  0.3f0.0f},        
                                                
new float[] {0.0f,  0.0f,  0.0f,  0.0f
【上篇】
【下篇】

抱歉!评论已关闭.