• 给图片添加水印效果图的函数(可以在图片上添加自己的版权和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.0f1.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(savePhotoPath, ImageFormat.Jpeg);
                imgPhoto.Dispose();
                imgWatermark.Dispose();

            }

        }

    }

    原文件来自:http://www.cnblogs.com/zhuhee/archive/2006/06/16/427358.html
    源码这里下载:https://files.cnblogs.com/zhuhee/watermark_source.rar
  • 相关阅读:
    html单引号,双引号转义
    把文章里边的html标签去掉(去掉文字的样式,显示css设置的样式)
    java缓存适合使用的情况
    Java内存缓存
    springmvc怎么重定向,从一个controller跳到另一个controller
    jquery 获取标签名(tagName)
    jQuery判断checkbox是否选中的3种方法
    判断一组checkbox中是否有被选中的
    在O(N)时间内求解 正数数组中 两个数相加的 最大值
    两种方法求解 正数数组中 两个数相减 的最大值
  • 原文地址:https://www.cnblogs.com/puke/p/773923.html
Copyright © 2020-2023  润新知