C# 添加 图片 水印

 

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

namespace  Tutorial
{
    
class WaterMark
    
{

        [STAThread]
        
static void Main(string[] args)
        
{
            
//设置工作目录
            string WorkingDirectory = @"C:Documents and Settingsadministrator.JAZZMINEMy DocumentsProjectsTutorialsWaterMark";

            
//定义版权声明的字符串信息或文本(看来原作者是老外)
            string Copyright = "Copyright @2002 - AP Photo/David Zalubowski";

            
//创建一个需要添加水印的图像对象
            Image imgPhoto = Image.FromFile(WorkingDirectory + "/watermark_photo.jpg");
            
int phWidth = imgPhoto.Width;
            
int phHeight = imgPhoto.Height;

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

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

            
//把Bitmap装载进Graphics对象 
            Graphics grPhoto = Graphics.FromImage(bmPhoto);

            
//创建一个包含水印的image对象
            Image imgWatermark = new Bitmap(WorkingDirectory + "/watermark.bmp");
            
int wmWidth = imgWatermark.Width;
            
int wmHeight = imgWatermark.Height;

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

            
//设置这个要转换Graphics对象的图像质量
            grPhoto.SmoothingMode = SmoothingMode.AntiAlias;

            
//绘制图像对象到graphics对象(保留原始的宽高)
            grPhoto.DrawImage(
                imgPhoto,                               
// Photo Image object
                new Rectangle(00, phWidth, phHeight), // 构造矩形
                0,                                      // 要绘制的源图像的x坐标 
                0,                                      // 要绘制的源图像的y坐标
                phWidth,                                // 要绘制的源图像的宽度
                phHeight,                               // 要绘制的源图像的高度
                GraphicsUnit.Pixel);                    // 图像所使用的单位

            
//-------------------------------------------------------
            
//为了最大化版权信息的字体,我们通过测试多个字体大小,定一个了一个数组来包含不同的字体大小
            
//-------------------------------------------------------
            int[] sizes = new int[]{16,14,12,10,8,6,4};

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

            
//通过循环这个数组,来选用不同的字体大小
            
//如果它的大小小于图像的宽度,就选用这个大小的字体
            for (int i=0 ;i<7; i++)
            
{
                
//设置字体,这里是用arial,黑体
                crFont = new Font("arial", sizes[i], FontStyle.Bold);
                
//Measure the Copyright string in this Font
                crSize = grPhoto.MeasureString(Copyright, crFont);

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


            
//因为图片的高度可能不尽相同, 所以定义了
            
//从图片底部算起预留了5%的空间
            int yPixlesFromBottom = (int)(phHeight *.05);

            
//现在使用版权信息字符串的高度来确定要绘制的图像的字符串的y坐标
 
            
float yPosFromBottom = ((phHeight - yPixlesFromBottom)-(crSize.Height/2));

            
//计算x坐标
            float xCenterOfImg = (phWidth/2);

            
//把文本布局设置为居中
            StringFormat StrFormat = new StringFormat();
            StrFormat.Alignment 
= StringAlignment.Center;

            
//通过Brush来设置黑色半透明
            SolidBrush semiTransBrush2 = new SolidBrush(Color.FromArgb(153000));

            
//绘制版权字符串
            grPhoto.DrawString(Copyright,                 //版权字符串文本
                crFont,                                   //字体
                semiTransBrush2,                           //Brush
                new PointF(xCenterOfImg+1,yPosFromBottom+1),  //位置
                StrFormat);

            
//设置成白色半透明
            SolidBrush semiTransBrush = new SolidBrush(Color.FromArgb(153255255255));

            
//第二次绘制版权字符串来创建阴影效果
            
//记住移动文本的位置1像素
            grPhoto.DrawString(Copyright,                 //版权文本
                crFont,                                   //字体
                semiTransBrush,                           //Brush
                new PointF(xCenterOfImg,yPosFromBottom),  //位置
                StrFormat);                               //文本对齐

            

            
//------------------------------------------------------------
            
//第二步:插入水印图像
            
//------------------------------------------------------------

            
//通过之前创建的Bitmap创建一个 Bitmap
            Bitmap bmWatermark = new Bitmap(bmPhoto);
            bmWatermark.SetResolution(imgPhoto.HorizontalResolution, imgPhoto.VerticalResolution);
            
            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();

            
//第一步是用水印来替代背景颜色 
            
//(Alpha=0, R=0, G=0, B=0)
            
//要使用 Colormap 来定义一个RemapTable
            ColorMap colorMap = new ColorMap();

            
//这个水印的背景定义成100%绿色,并用来替代透明           
            colorMap.OldColor = Color.FromArgb(25502550);
            colorMap.NewColor 
= Color.FromArgb(0000); 

            ColorMap[] remapTable 
= {colorMap};

            imageAttributes.SetRemapTable(remapTable, ColorAdjustType.Bitmap);

            
//第二个颜色是用来控制水印图片的不透明度的 
            
//是使用一个5*5的RGBA矩阵来实现的 
            
// 设置第4行、第4列的值为0.3来不透明度的级别
            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.0f1.0f}}

            ColorMatrix wmColorMatrix 
= new ColorMatrix(colorMatrixElements);

            imageAttributes.SetColorMatrix(wmColorMatrix, ColorMatrixFlag.Default,
                ColorAdjustType.Bitmap);

            
//本例的水印放置在右上方
            
// 偏移量是10
            

            
int xPosOfWm = ((phWidth - wmWidth)-10);
            
int yPosOfWm = 10;

            grWatermark.DrawImage(imgWatermark, 
                
new Rectangle(xPosOfWm,yPosOfWm,wmWidth,wmHeight),  //Set the detination Position
                0,                  // x坐标
                0,                  // y坐标
                wmWidth,            // 水印宽度
                wmHeight,            // 水印高度
                GraphicsUnit.Pixel, // 单位
                imageAttributes);   //ImageAttributes对象

            
//使用新生成的加了水印图片替代原始图片
            imgPhoto = bmWatermark;
            grPhoto.Dispose();
            grWatermark.Dispose();

            
//保存的路径
            imgPhoto.Save(WorkingDirectory + "/watermark_final.jpg", ImageFormat.Jpeg);
            imgPhoto.Dispose();
            imgWatermark.Dispose();

        }

    }

}


            colorMap.NewColor = Color.FromArgb(0, 0, 0, 0);

            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.0f, 0.0f},      
                                                new float[] {0.0f,  1.0f,  0.0f,  0.0f, 0.0f},       
                                                new float[] {0.0f,  0.0f,  1.0f,  0.0f, 0.0f},       
                                                new float[] {0.0f,  0.0f,  0.0f,  0.3f, 0.0f},       
                                                new float[] {0.0f,  0.0f,  0.0f,  0.0f, 1.0f}};
            ColorMatrix wmColorMatrix = new ColorMatrix(colorMatrixElements);

            imageAttributes.SetColorMatrix(wmColorMatrix, ColorMatrixFlag.Default,
                ColorAdjustType.Bitmap);

            //For this example we will place the watermark in the upper right
            //hand corner of the photograph. offset down 10 pixels and to the
            //left 10 pixles

            int xPosOfWm = ((phWidth - wmWidth)-10);
            int yPosOfWm = 10;

            grWatermark.DrawImage(imgWatermark,
                new Rectangle(xPosOfWm,yPosOfWm,wmWidth,wmHeight),  //Set the detination Position
                0,                  // x-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
                wmHeight,            // Watermark Height
                GraphicsUnit.Pixel, // Unit of measurment
                imageAttributes);   //ImageAttributes Object

            //Replace the original photgraphs bitmap with the new Bitmap
            imgPhoto = bmWatermark;
            grPhoto.Dispose();
            grWatermark.Dispose();

            //save new image to file system.
            imgPhoto.Save(WorkingDirectory + "//watermark_final.jpg", ImageFormat.Jpeg);
            imgPhoto.Dispose();
            imgWatermark.Dispose();

        }
    }
}

评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值