给图片添加水印效果
using System.Drawing;
 using System.Drawing.Imaging;
using System.Drawing.Imaging; using System.Drawing.Drawing2D;
using System.Drawing.Drawing2D;


 /**//// <summary>
/**//// <summary> /// 用于处理图片
        /// 用于处理图片 /// 给图片加入水印
        /// 给图片加入水印 /// </summary>
        /// </summary> /// <param name="Copyright">需要写入的版权信息</param>
        /// <param name="Copyright">需要写入的版权信息</param> /// <param name="MarkBmpPath">需要假如的logo图片</param>
        /// <param name="MarkBmpPath">需要假如的logo图片</param> /// <param name="photoPath">需要处理的图片的路径</param>
        /// <param name="photoPath">需要处理的图片的路径</param> /// <param name="savePhotoPath">保存的图片路径</param>
        /// <param name="savePhotoPath">保存的图片路径</param> public void MakeWatermark(string Copyright,string MarkBmpPath,string photoPath,string savePhotoPath)
        public void MakeWatermark(string Copyright,string MarkBmpPath,string photoPath,string savePhotoPath)
 
         {
{
 //创建一个image对象,即要被处理的图片
            //创建一个image对象,即要被处理的图片 Image imgPhoto = Image.FromFile(photoPath);
            Image imgPhoto = Image.FromFile(photoPath); int phWidth = imgPhoto.Width;
            int phWidth = imgPhoto.Width; int phHeight = imgPhoto.Height;
            int phHeight = imgPhoto.Height;
 //创建原始图片大小的Bitmap
            //创建原始图片大小的Bitmap Bitmap bmPhoto = new Bitmap(phWidth, phHeight, PixelFormat.Format24bppRgb);
            Bitmap bmPhoto = new Bitmap(phWidth, phHeight, PixelFormat.Format24bppRgb);
 bmPhoto.SetResolution(imgPhoto.HorizontalResolution, imgPhoto.VerticalResolution);
            bmPhoto.SetResolution(imgPhoto.HorizontalResolution, imgPhoto.VerticalResolution);
 //将位图bmPhoto加载到Graphics对象
            //将位图bmPhoto加载到Graphics对象 Graphics grPhoto = Graphics.FromImage(bmPhoto);
            Graphics grPhoto = Graphics.FromImage(bmPhoto);
 //创建一个需要填充水银的Image对象
            //创建一个需要填充水银的Image对象 Image imgWatermark = new Bitmap(MarkBmpPath);
            Image imgWatermark = new Bitmap(MarkBmpPath); int wmWidth = imgWatermark.Width;
            int wmWidth = imgWatermark.Width; int wmHeight = imgWatermark.Height;
            int wmHeight = imgWatermark.Height;
 //------------------------------------------------------------
            //------------------------------------------------------------ //第一步,插入版权信息
            //第一步,插入版权信息 //------------------------------------------------------------
            //------------------------------------------------------------
 //设置图象的成相质量
            //设置图象的成相质量 grPhoto.SmoothingMode = SmoothingMode.AntiAlias;
            grPhoto.SmoothingMode = SmoothingMode.AntiAlias;
 //将原始图象绘制到grPhoto上
            //将原始图象绘制到grPhoto上 grPhoto.DrawImage(
            grPhoto.DrawImage( imgPhoto,                               // 要绘制的Image对象
                imgPhoto,                               // 要绘制的Image对象 new Rectangle(0, 0, phWidth, phHeight), // 绘制图象的位置和大小
                new Rectangle(0, 0, phWidth, phHeight), // 绘制图象的位置和大小 0,                                      // 要绘制的原图象部分的左上角的X坐标
                0,                                      // 要绘制的原图象部分的左上角的X坐标 0,                                      // 要绘制的原图象部分的左上角的Y坐标
                0,                                      // 要绘制的原图象部分的左上角的Y坐标 phWidth,                                // 要绘制的原图象的高度
                phWidth,                                // 要绘制的原图象的高度 phHeight,                               // 要绘制的原图象的宽度
                phHeight,                               // 要绘制的原图象的宽度 GraphicsUnit.Pixel);                    // 源矩形的度量单位
                GraphicsUnit.Pixel);                    // 源矩形的度量单位
 //-------------------------------------------------------
            //------------------------------------------------------- //字体大小放在一个数组中,最大字体为32.
            //字体大小放在一个数组中,最大字体为32. //感觉用处不大,可以自己再修改这里
            //感觉用处不大,可以自己再修改这里 //-------------------------------------------------------
            //-------------------------------------------------------
 int[] sizes = new int[]
            int[] sizes = new int[] {32,14,12,10,8,6,4};
{32,14,12,10,8,6,4};
 Font crFont = null;
            Font crFont = null; SizeF crSize = new SizeF();
            SizeF crSize = new SizeF();
 //循环测试数组中所定义的字体大小是否适合版权信息,如果合适就使用此种字体大小
            //循环测试数组中所定义的字体大小是否适合版权信息,如果合适就使用此种字体大小 for (int i=0 ;i<6; i++)
            for (int i=0 ;i<6; i++)
 
             {
{ //设置字体类型,可以单独提出,作为参数
                //设置字体类型,可以单独提出,作为参数 crFont = new Font("迷你繁篆书", sizes[0], FontStyle.Bold);
                crFont = new Font("迷你繁篆书", sizes[0], FontStyle.Bold); //测量此种字体大小
                //测量此种字体大小 crSize = grPhoto.MeasureString(Copyright, crFont);
                crSize = grPhoto.MeasureString(Copyright, crFont);
 if((ushort)crSize.Width < (ushort)phWidth)
                if((ushort)crSize.Width < (ushort)phWidth) break;
                    break; }
            }
 //给底部保留%3的空间
            //给底部保留%3的空间 int yPixlesFromBottom = (int)(phHeight *.03);
            int yPixlesFromBottom = (int)(phHeight *.03);
 //设置字体在图片中的位置
            //设置字体在图片中的位置 float yPosFromBottom = ((phHeight - yPixlesFromBottom)-(crSize.Height/2));
            float yPosFromBottom = ((phHeight - yPixlesFromBottom)-(crSize.Height/2));
 //float xCenterOfImg = (phWidth/2);
            //float xCenterOfImg = (phWidth/2); float xCenterOfImg = (phWidth-(crSize.Width)/2);
            float xCenterOfImg = (phWidth-(crSize.Width)/2); //设置字体居中
            //设置字体居中 StringFormat StrFormat = new StringFormat();
            StringFormat StrFormat = new StringFormat(); StrFormat.Alignment = StringAlignment.Center;
            StrFormat.Alignment = StringAlignment.Center; 
             //设置绘制文本的颜色和纹理 (Alpha=153)
            //设置绘制文本的颜色和纹理 (Alpha=153) SolidBrush semiTransBrush2 = new SolidBrush(Color.FromArgb(153, 0, 0, 0));
            SolidBrush semiTransBrush2 = new SolidBrush(Color.FromArgb(153, 0, 0, 0));
 //将版权信息绘制到图象上
            //将版权信息绘制到图象上 grPhoto.DrawString(Copyright,                     //版权信息
            grPhoto.DrawString(Copyright,                     //版权信息 crFont,                                       //字体
                crFont,                                       //字体 semiTransBrush2,                              //绘制文本的颜色及纹理
                semiTransBrush2,                              //绘制文本的颜色及纹理 new PointF(xCenterOfImg+1,yPosFromBottom+1),  //绘制文本的位置
                new PointF(xCenterOfImg+1,yPosFromBottom+1),  //绘制文本的位置 StrFormat);                                   //格式
                StrFormat);                                   //格式
 //设置绘制文本的颜色和纹理 (Alpha=153)
            //设置绘制文本的颜色和纹理 (Alpha=153) SolidBrush semiTransBrush = new SolidBrush(Color.FromArgb(153, 255, 255, 255));
            SolidBrush semiTransBrush = new SolidBrush(Color.FromArgb(153, 255, 255, 255));
 //重新绘制版权信息,以让其具有阴影效果
            //重新绘制版权信息,以让其具有阴影效果 //将文本向又下移动一个象素
            //将文本向又下移动一个象素 grPhoto.DrawString(Copyright,                 //版权信息
            grPhoto.DrawString(Copyright,                 //版权信息 crFont,                                   //字体
                crFont,                                   //字体 semiTransBrush,                           //绘制文本的颜色及纹理
                semiTransBrush,                           //绘制文本的颜色及纹理 new PointF(xCenterOfImg,yPosFromBottom),  //绘制文本的位置
                new PointF(xCenterOfImg,yPosFromBottom),  //绘制文本的位置 StrFormat);                               //格式
                StrFormat);                               //格式
 
            
 //------------------------------------------------------------
            //------------------------------------------------------------ //第二步,插入图片水印
            //第二步,插入图片水印 //------------------------------------------------------------
            //------------------------------------------------------------
 //在原来修改过的bmPhoto上创建一个水银位图
            //在原来修改过的bmPhoto上创建一个水银位图 Bitmap bmWatermark = new Bitmap(bmPhoto);
            Bitmap bmWatermark = new Bitmap(bmPhoto);
 bmWatermark.SetResolution(imgPhoto.HorizontalResolution, imgPhoto.VerticalResolution);
            bmWatermark.SetResolution(imgPhoto.HorizontalResolution, imgPhoto.VerticalResolution); //将位图bmWatermark加载到Graphics对象
            //将位图bmWatermark加载到Graphics对象 Graphics grWatermark = Graphics.FromImage(bmWatermark);
            Graphics grWatermark = Graphics.FromImage(bmWatermark);
 //To achieve a transulcent watermark we will apply (2) color
            //To achieve a transulcent watermark we will apply (2) color  //manipulations by defineing a ImageAttributes object and
            //manipulations by defineing a ImageAttributes object and  //seting (2) of its properties.
            //seting (2) of its properties. ImageAttributes imageAttributes = new ImageAttributes();
            ImageAttributes imageAttributes = new ImageAttributes();
 //The first step in manipulating the watermark image is to replace
            //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)
            //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
            //to do this we will use a Colormap and use this to define a RemapTable ColorMap colorMap = new ColorMap();
            ColorMap colorMap = new ColorMap();
 //My watermark was defined with a background of 100% Green this will
            //My watermark was defined with a background of 100% Green this will //be the color we search for and replace with transparency
            //be the color we search for and replace with transparency colorMap.OldColor = Color.FromArgb(255, 0, 255, 0);
            colorMap.OldColor = Color.FromArgb(255, 0, 255, 0); colorMap.NewColor = Color.FromArgb(0, 0, 0, 0);
            colorMap.NewColor = Color.FromArgb(0, 0, 0, 0);

 ColorMap[] remapTable =
            ColorMap[] remapTable =  { colorMap };
{ colorMap };
 imageAttributes.SetRemapTable(remapTable, ColorAdjustType.Bitmap);
            imageAttributes.SetRemapTable(remapTable, ColorAdjustType.Bitmap);
 //The second color manipulation is used to change the opacity of the
            //The second color manipulation is used to change the opacity of the  //watermark.  This is done by applying a 5x5 matrix that contains 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
            //coordinates for the RGBA space.  By setting the 3rd row and 3rd column  //to 0.3f we achive a level of opacity
            //to 0.3f we achive a level of opacity
 float[][] colorMatrixElements =
            float[][] colorMatrixElements =  {
{ 
 new float[]
                                                new float[]  {1.0f,  0.0f,  0.0f,  0.0f, 0.0f},
{1.0f,  0.0f,  0.0f,  0.0f, 0.0f},       
 new float[]
                                                new float[]  {0.0f,  1.0f,  0.0f,  0.0f, 0.0f},
{0.0f,  1.0f,  0.0f,  0.0f, 0.0f},        
 new float[]
                                                new float[]  {0.0f,  0.0f,  1.0f,  0.0f, 0.0f},
{0.0f,  0.0f,  1.0f,  0.0f, 0.0f},        
 new float[]
                                                new float[]  {0.0f,  0.0f,  0.0f,  0.3f, 0.0f},
{0.0f,  0.0f,  0.0f,  0.3f, 0.0f},        
 new float[]
                                                new float[]  {0.0f,  0.0f,  0.0f,  0.0f, 1.0f}};
{0.0f,  0.0f,  0.0f,  0.0f, 1.0f}}; ColorMatrix wmColorMatrix = new ColorMatrix(colorMatrixElements);
            ColorMatrix wmColorMatrix = new ColorMatrix(colorMatrixElements);
 imageAttributes.SetColorMatrix(wmColorMatrix, ColorMatrixFlag.Default,
            imageAttributes.SetColorMatrix(wmColorMatrix, ColorMatrixFlag.Default, ColorAdjustType.Bitmap);
                ColorAdjustType.Bitmap);
 //For this example we will place the watermark in the upper right
            //For this example we will place the watermark in the upper right //hand corner of the photograph. offset down 10 pixels and to the
            //hand corner of the photograph. offset down 10 pixels and to the  //left 10 pixles
            //left 10 pixles
 int xPosOfWm = ((phWidth - wmWidth) - 10);
            int xPosOfWm = ((phWidth - wmWidth) - 10); int yPosOfWm = 10;
            int yPosOfWm = 10;
 grWatermark.DrawImage(imgWatermark,
            grWatermark.DrawImage(imgWatermark, new Rectangle(xPosOfWm, yPosOfWm, wmWidth, wmHeight),  //Set the detination Position
                new Rectangle(xPosOfWm, yPosOfWm, wmWidth, wmHeight),  //Set the detination Position 0,                  // x-coordinate of the portion of the source image to draw.
                0,                  // x-coordinate of the portion of the source image to draw.  0,                  // y-coordinate of the portion of the source image to draw.
                0,                  // y-coordinate of the portion of the source image to draw.  wmWidth,            // Watermark Width
                wmWidth,            // Watermark Width wmHeight,            // Watermark Height
                wmHeight,            // Watermark Height GraphicsUnit.Pixel, // Unit of measurment
                GraphicsUnit.Pixel, // Unit of measurment imageAttributes);   //ImageAttributes Object
                imageAttributes);   //ImageAttributes Object
 //Replace the original photgraphs bitmap with the new Bitmap
            //Replace the original photgraphs bitmap with the new Bitmap imgPhoto = bmWatermark;
            imgPhoto = bmWatermark; grPhoto.Dispose();
            grPhoto.Dispose(); grWatermark.Dispose();
            grWatermark.Dispose();
 //save new image to file system.
            //save new image to file system. imgPhoto.Save(savePhotoPath, ImageFormat.Jpeg);
            imgPhoto.Save(savePhotoPath, ImageFormat.Jpeg); imgPhoto.Dispose();
            imgPhoto.Dispose(); imgWatermark.Dispose();
            imgWatermark.Dispose();
 }
        } }
    } }
}作者:Bober Song
出处:http://bober.cnblogs.com/
CARE健康网: http://www.aicareyou.com/
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
出处:http://bober.cnblogs.com/
CARE健康网: http://www.aicareyou.com/
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
 
                    
                
 
                
            
         浙公网安备 33010602011771号
浙公网安备 33010602011771号