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

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

原图:

效果图:

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

None.gif
using  System;
None.gif
using  System.Drawing;
None.gif
using  System.Drawing.Imaging;
None.gif
using  System.Drawing.Drawing2D;
None.gif
None.gif
namespace  WebWaterMark.Components
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// <summary>
InBlock.gif    
/// watermark 的摘要说明。
ExpandedSubBlockEnd.gif    
/// </summary>

InBlock.gif    public class watermark
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
public watermark()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
//
InBlock.gif            
// TODO: 在此处添加构造函数逻辑
InBlock.gif            
//
ExpandedSubBlockEnd.gif
        }

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

InBlock.gif        public void MakeWatermark(string Copyright,string MarkBmpPath,string photoPath,string savePhotoPath)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif
InBlock.gif            
//创建一个image对象,即要被处理的图片
InBlock.gif
            Image imgPhoto = Image.FromFile(photoPath);
InBlock.gif            
int phWidth = imgPhoto.Width;
InBlock.gif            
int phHeight = imgPhoto.Height;
InBlock.gif
InBlock.gif            
//创建原始图片大小的Bitmap
InBlock.gif
            Bitmap bmPhoto = new Bitmap(phWidth, phHeight, PixelFormat.Format24bppRgb);
InBlock.gif
InBlock.gif            bmPhoto.SetResolution(imgPhoto.HorizontalResolution, imgPhoto.VerticalResolution);
InBlock.gif
InBlock.gif            
//将位图bmPhoto加载到Graphics对象
InBlock.gif
            Graphics grPhoto = Graphics.FromImage(bmPhoto);
InBlock.gif
InBlock.gif            
//创建一个需要填充水银的Image对象
InBlock.gif
            Image imgWatermark = new Bitmap(MarkBmpPath);
InBlock.gif            
int wmWidth = imgWatermark.Width;
InBlock.gif            
int wmHeight = imgWatermark.Height;
InBlock.gif
InBlock.gif            
//------------------------------------------------------------
InBlock.gif            
//第一步,插入版权信息
InBlock.gif            
//------------------------------------------------------------
InBlock.gif
InBlock.gif            
//设置图象的成相质量
InBlock.gif
            grPhoto.SmoothingMode = SmoothingMode.AntiAlias;
InBlock.gif
InBlock.gif            
//将原始图象绘制到grPhoto上
InBlock.gif
            grPhoto.DrawImage(
InBlock.gif                imgPhoto,                               
// 要绘制的Image对象
InBlock.gif
                new Rectangle(00, phWidth, phHeight), // 绘制图象的位置和大小
InBlock.gif
                0,                                      // 要绘制的原图象部分的左上角的X坐标
InBlock.gif
                0,                                      // 要绘制的原图象部分的左上角的Y坐标
InBlock.gif
                phWidth,                                // 要绘制的原图象的高度
InBlock.gif
                phHeight,                               // 要绘制的原图象的宽度
InBlock.gif
                GraphicsUnit.Pixel);                    // 源矩形的度量单位
InBlock.gif
InBlock.gif            
//-------------------------------------------------------
InBlock.gif            
//字体大小放在一个数组中,最大字体为32.
InBlock.gif            
//感觉用处不大,可以自己再修改这里
InBlock.gif            
//-------------------------------------------------------
ExpandedSubBlockStart.gifContractedSubBlock.gif
            int[] sizes = new int[]dot.gif{8,7,6,5,4};
InBlock.gif
InBlock.gif            Font crFont 
= null;
InBlock.gif            SizeF crSize 
= new SizeF();
InBlock.gif
InBlock.gif            
//循环测试数组中所定义的字体大小是否适合版权信息,如果合适就使用此种字体大小
InBlock.gif
            for (int i=0 ;i<5; i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
//设置字体类型,可以单独提出,作为参数
InBlock.gif
                crFont = new Font("宋体", sizes[i], FontStyle.Bold);
InBlock.gif                
//测量此种字体大小
InBlock.gif
                crSize = grPhoto.MeasureString(Copyright, crFont);
InBlock.gif
InBlock.gif                
if((ushort)crSize.Width < (ushort)phWidth)
InBlock.gif                    
break;
ExpandedSubBlockEnd.gif            }

InBlock.gif
InBlock.gif            
//给底部保留%3的空间
InBlock.gif
            int yPixlesFromBottom = (int)(phHeight *.03);
InBlock.gif
InBlock.gif            
//设置字体在图片中的位置
InBlock.gif
            float yPosFromBottom = ((phHeight - yPixlesFromBottom)-(crSize.Height/2));
InBlock.gif
InBlock.gif            
//float xCenterOfImg = (phWidth/2);
InBlock.gif
            float xCenterOfImg = (phWidth-(crSize.Width)/2);
InBlock.gif            
//设置字体居中
InBlock.gif
            StringFormat StrFormat = new StringFormat();
InBlock.gif            StrFormat.Alignment 
= StringAlignment.Center;
InBlock.gif            
InBlock.gif            
//设置绘制文本的颜色和纹理 (Alpha=153)
InBlock.gif
            SolidBrush semiTransBrush2 = new SolidBrush(Color.FromArgb(153000));
InBlock.gif
InBlock.gif            
//将版权信息绘制到图象上
InBlock.gif
            grPhoto.DrawString(Copyright,                     //版权信息
InBlock.gif
                crFont,                                       //字体
InBlock.gif
                semiTransBrush2,                              //绘制文本的颜色及纹理
InBlock.gif
                new PointF(xCenterOfImg+1,yPosFromBottom+1),  //绘制文本的位置
InBlock.gif
                StrFormat);                                   //格式
InBlock.gif
InBlock.gif            
//设置绘制文本的颜色和纹理 (Alpha=153)
InBlock.gif
            SolidBrush semiTransBrush = new SolidBrush(Color.FromArgb(153255255255));
InBlock.gif
InBlock.gif            
//重新绘制版权信息,以让其具有阴影效果
InBlock.gif            
//将文本向又下移动一个象素
InBlock.gif
            grPhoto.DrawString(Copyright,                 //版权信息
InBlock.gif
                crFont,                                   //字体
InBlock.gif
                semiTransBrush,                           //绘制文本的颜色及纹理
InBlock.gif
                new PointF(xCenterOfImg,yPosFromBottom),  //绘制文本的位置
InBlock.gif
                StrFormat);                               //格式
InBlock.gif
InBlock.gif            
InBlock.gif
InBlock.gif            
//------------------------------------------------------------
InBlock.gif            
//第二步,插入图片水印
InBlock.gif            
//------------------------------------------------------------
InBlock.gif
InBlock.gif            
//在原来修改过的bmPhoto上创建一个水银位图
InBlock.gif
            Bitmap bmWatermark = new Bitmap(bmPhoto);
InBlock.gif
InBlock.gif            bmWatermark.SetResolution(imgPhoto.HorizontalResolution, imgPhoto.VerticalResolution);
InBlock.gif            
//将位图bmWatermark加载到Graphics对象
InBlock.gif
            Graphics grWatermark = Graphics.FromImage(bmWatermark);
InBlock.gif
InBlock.gif            
//To achieve a transulcent watermark we will apply (2) color 
InBlock.gif            
//manipulations by defineing a ImageAttributes object and 
InBlock.gif            
//seting (2) of its properties.
InBlock.gif
            ImageAttributes imageAttributes = new ImageAttributes();
InBlock.gif
InBlock.gif            
//The first step in manipulating the watermark image is to replace 
InBlock.gif            
//the background color with one that is trasparent (Alpha=0, R=0, G=0, B=0)
InBlock.gif            
//to do this we will use a Colormap and use this to define a RemapTable
InBlock.gif
            ColorMap colorMap = new ColorMap();
InBlock.gif
InBlock.gif            
//My watermark was defined with a background of 100% Green this will
InBlock.gif            
//be the color we search for and replace with transparency
InBlock.gif
            colorMap.OldColor = Color.FromArgb(25502550);
InBlock.gif            colorMap.NewColor 
= Color.FromArgb(0000); 
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif            ColorMap[] remapTable 
= dot.gif{colorMap};
InBlock.gif
InBlock.gif            imageAttributes.SetRemapTable(remapTable, ColorAdjustType.Bitmap);
InBlock.gif
InBlock.gif            
//The second color manipulation is used to change the opacity of the 
InBlock.gif            
//watermark.  This is done by applying a 5x5 matrix that contains the 
InBlock.gif            
//coordinates for the RGBA space.  By setting the 3rd row and 3rd column 
InBlock.gif            
//to 0.3f we achive a level of opacity
ExpandedSubBlockStart.gifContractedSubBlock.gif
            float[][] colorMatrixElements = dot.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif                                                
new float[] dot.gif{1.0f,  0.0f,  0.0f,  0.0f0.0f},       
ExpandedSubBlockStart.gifContractedSubBlock.gif                                                
new float[] dot.gif{0.0f,  1.0f,  0.0f,  0.0f0.0f},        
ExpandedSubBlockStart.gifContractedSubBlock.gif                                                
new float[] dot.gif{0.0f,  0.0f,  1.0f,  0.0f0.0f},        
ExpandedSubBlockStart.gifContractedSubBlock.gif                                                
new float[] dot.gif{0.0f,  0.0f,  0.0f,  0.3f0.0f},        
ExpandedSubBlockStart.gifContractedSubBlock.gif                                                
new float[] dot.gif{0.0f,  0.0f,  0.0f,  0.0f1.0f}}

InBlock.gif            ColorMatrix wmColorMatrix 
= new ColorMatrix(colorMatrixElements);
InBlock.gif
InBlock.gif            imageAttributes.SetColorMatrix(wmColorMatrix, ColorMatrixFlag.Default,
InBlock.gif                ColorAdjustType.Bitmap);
InBlock.gif
InBlock.gif            
//For this example we will place the watermark in the upper right
InBlock.gif            
//hand corner of the photograph. offset down 10 pixels and to the 
InBlock.gif            
//left 10 pixles
InBlock.gif

InBlock.gif            
int xPosOfWm = ((phWidth - wmWidth)-10);
InBlock.gif            
int yPosOfWm = 10;
InBlock.gif
InBlock.gif            grWatermark.DrawImage(imgWatermark, 
InBlock.gif                
new Rectangle(xPosOfWm,yPosOfWm,wmWidth,wmHeight),  //Set the detination Position
InBlock.gif
                0,                  // x-coordinate of the portion of the source image to draw. 
InBlock.gif
                0,                  // y-coordinate of the portion of the source image to draw. 
InBlock.gif
                wmWidth,            // Watermark Width
InBlock.gif
                wmHeight,            // Watermark Height
InBlock.gif
                GraphicsUnit.Pixel, // Unit of measurment
InBlock.gif
                imageAttributes);   //ImageAttributes Object
InBlock.gif
InBlock.gif            
//Replace the original photgraphs bitmap with the new Bitmap
InBlock.gif
            imgPhoto = bmWatermark;
InBlock.gif            grPhoto.Dispose();
InBlock.gif            grWatermark.Dispose();
InBlock.gif
InBlock.gif            
//save new image to file system.
InBlock.gif
            imgPhoto.Save(savePhotoPath, ImageFormat.Jpeg);
InBlock.gif            imgPhoto.Dispose();
InBlock.gif            imgWatermark.Dispose();
InBlock.gif
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif

转载于:https://www.cnblogs.com/zhuhee/archive/2006/06/16/427358.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值