/*
 * Class:WaterImage
 * Use for add a water Image to the picture both words and image
 * By Gary Gong From Demetersoft.com | 和信诚软件(北京)有限公司
 * 2007.07.23   create the file 
 *
 *
 * 使用说明:
 *  建议先定义一个WaterImage实例
 *  然后利用实例的属性,去匹配需要进行操作的参数
 *  然后定义一个WaterImageManage实例
 *  利用WaterImageManage实例进行DrawImage(),印图片水印
 *  DrawWords()印文字水印
 *
-*/

using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.Drawing.Drawing2D;
using System.IO;
/// <summary>
/// 图片位置
/// </summary>
public enum ImagePosition
{
    LeftTop,        //左上
    LeftBottom,    //左下
    RightTop,       //右上
    RigthBottom,  //右下
    TopMiddle,     //顶部居中
    BottomMiddle, //底部居中
    Center           //中心
}

/// <summary>
/// 水印图片的操作管理 Design by Gary Gong From Demetersoft.com
/// </summary>
public class WaterImageManage
{
    /// <summary>
    /// 生成一个新的水印图片制作实例
    /// </summary>
 public WaterImageManage ()
 {
  //
  // TODO: Add constructor logic here
  //
 }

/// <summary>
    /// 添加图片水印
    /// </summary>
    /// <param name="sourcePicture">源图片文件名</param>
    /// <param name="waterImage">水印图片文件名</param>
    /// <param name="alpha">透明度(0.1-1.0数值越小透明度越高)</param>
    /// <param name="position">位置</param>
    /// <param name="PicturePath" >图片的路径</param>
    /// <returns>返回生成于指定文件夹下的水印文件名</returns>
    public string  DrawImage(string sourcePicture,
                                      string waterImage,
                                      float alpha,
                                      ImagePosition position,
                                      string PicturePath )
    {
        //
        // 判断参数是否有效
        //
        if (sourcePicture == string.Empty || waterImage == string.Empty || alpha == 0.0 || PicturePath == string.Empty)
        {
            return sourcePicture;
        }

//
        // 源图片,水印图片全路径
        //
        string sourcePictureName = PicturePath + sourcePicture;
        string waterPictureName = PicturePath + waterImage;
        string fileSourceExtension = System.IO.Path.GetExtension(sourcePictureName).ToLower();
        string fileWaterExtension = System.IO.Path.GetExtension(waterPictureName).ToLower();
        //
        // 判断文件是否存在,以及类型是否正确
        //
        if (System.IO.File.Exists(sourcePictureName) == false ||
            System.IO.File.Exists(waterPictureName) == false ||(
            fileSourceExtension != ".gif" &&
            fileSourceExtension != ".jpg" &&
            fileSourceExtension != ".png") || (
            fileWaterExtension != ".gif" &&
            fileWaterExtension != ".jpg" &&
            fileWaterExtension != ".png")
            )
        {
            return sourcePicture;
        }

//
        // 目标图片名称及全路径
        //
        string targetImage = sourcePictureName.Replace ( System.IO.Path.GetExtension(sourcePictureName),"") + "_1101.jpg";

//
        // 将需要加上水印的图片装载到Image对象中
        //
        Image imgPhoto = Image.FromFile(sourcePictureName);
        //
        // 确定其长宽
        //
        int phWidth = imgPhoto.Width;
        int phHeight = imgPhoto.Height;

//
        // 封装 GDI+ 位图,此位图由图形图像及其属性的像素数据组成。
        //
        Bitmap bmPhoto = new Bitmap(phWidth, phHeight, PixelFormat.Format24bppRgb);

//
        // 设定分辨率
        //
        bmPhoto.SetResolution(imgPhoto.HorizontalResolution, imgPhoto.VerticalResolution);

//
        // 定义一个绘图画面用来装载位图
        //
        Graphics grPhoto = Graphics.FromImage(bmPhoto);

//
        //同样,由于水印是图片,我们也需要定义一个Image来装载它
        //
        Image imgWatermark = new Bitmap(waterPictureName);
       
        //
        // 获取水印图片的高度和宽度
        //
        int wmWidth = imgWatermark.Width;
        int wmHeight = imgWatermark.Height;

//SmoothingMode:指定是否将平滑处理(消除锯齿)应用于直线、曲线和已填充区域的边缘。
        // 成员名称   说明
        // AntiAlias      指定消除锯齿的呈现。 
        // Default        指定不消除锯齿。 
        // HighQuality  指定高质量、低速度呈现。 
        // HighSpeed   指定高速度、低质量呈现。 
        // Invalid        指定一个无效模式。 
        // None          指定不消除锯齿。
        grPhoto.SmoothingMode = SmoothingMode.AntiAlias;

//
        // 第一次描绘,将我们的底图描绘在绘图画面上
        //
        grPhoto.DrawImage(imgPhoto,                                         
                                    new Rectangle(0, 0, phWidth, phHeight),
                                    0,    
                                    0,      
                                    phWidth,  
                                    phHeight,     
                                    GraphicsUnit.Pixel);

//
        // 与底图一样,我们需要一个位图来装载水印图片。并设定其分辨率
        //
        Bitmap bmWatermark = new Bitmap(bmPhoto);
        bmWatermark.SetResolution(imgPhoto.HorizontalResolution, imgPhoto.VerticalResolution);
       
        //
        // 继续,将水印图片装载到一个绘图画面grWatermark
        //
        Graphics grWatermark = Graphics.FromImage(bmWatermark);

//
        //ImageAttributes 对象包含有关在呈现时如何操作位图和图元文件颜色的信息。
        //      
        ImageAttributes imageAttributes = new ImageAttributes();

//
        //Colormap: 定义转换颜色的映射
        //
        ColorMap colorMap = new ColorMap();

//
        //我的水印图被定义成拥有绿色背景色的图片被替换成透明
        //
        colorMap.OldColor = Color.FromArgb(255, 0, 255, 0);
        colorMap.NewColor = Color.FromArgb(0, 0, 0, 0);

ColorMap[] remapTable = { colorMap };

imageAttributes.SetRemapTable(remapTable, ColorAdjustType.Bitmap);

float[][] colorMatrixElements = {
           new float[] {1.0f,  0.0f,  0.0f,  0.0f, 0.0f}, // red红色
           new float[] {0.0f,  1.0f,  0.0f,  0.0f, 0.0f}, //green绿色
           new float[] {0.0f,  0.0f,  1.0f,  0.0f, 0.0f}, //blue蓝色      
           new float[] {0.0f,  0.0f,  0.0f,  alpha, 0.0f}, //透明度    
           new float[] {0.0f,  0.0f,  0.0f,  0.0f, 1.0f}};//

//  ColorMatrix:定义包含 RGBA 空间坐标的 5 x 5 矩阵。
        //  ImageAttributes 类的若干方法通过使用颜色矩阵调整图像颜色。
        ColorMatrix wmColorMatrix = new ColorMatrix(colorMatrixElements);

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

//
        //上面设置完颜色,下面开始设置位置
        //
        int xPosOfWm;
        int yPosOfWm;

switch (position)
        {
            case ImagePosition .BottomMiddle :
                xPosOfWm = (phWidth-wmWidth ) / 2 ;
                yPosOfWm = phHeight- wmHeight -10;
                break ;
            case ImagePosition .Center :
                xPosOfWm = (phWidth - wmWidth) / 2;
                yPosOfWm = (phHeight-wmHeight ) / 2;
                break ;
            case ImagePosition .LeftBottom :
                xPosOfWm = 10;
                yPosOfWm = phHeight - wmHeight - 10;
                break ;
            case ImagePosition .LeftTop :
                xPosOfWm = 10;
                yPosOfWm = 10;
                break;
            case ImagePosition .RightTop :
                xPosOfWm = phWidth - wmWidth - 10;
                yPosOfWm = 10;
                break ;
            case ImagePosition .RigthBottom :
                xPosOfWm = phWidth - wmWidth - 10;
                yPosOfWm = phHeight - wmHeight - 10;
                break ;
            case ImagePosition.TopMiddle :
                xPosOfWm = (phWidth - wmWidth) / 2;
                yPosOfWm = 10;
                break ;
            default:
                xPosOfWm = 10;
                yPosOfWm = phHeight - wmHeight - 10;
                break;
        }

//
        // 第二次绘图,把水印印上去
        //
        grWatermark.DrawImage(imgWatermark,
         new Rectangle(xPosOfWm,
                             yPosOfWm,
                             wmWidth,
                             wmHeight),
                             0,             
                             0,         
                             wmWidth, 
                             wmHeight,  
                             GraphicsUnit.Pixel, 
                             imageAttributes);

imgPhoto = bmWatermark;
        grPhoto.Dispose();
        grWatermark.Dispose();

//
        // 保存文件到服务器的文件夹里面
        //
        imgPhoto.Save(targetImage, ImageFormat.Jpeg);
        imgPhoto.Dispose();
        imgWatermark.Dispose();
        return targetImage.Replace (PicturePath,"");
    }

/// <summary>
    /// 在图片上添加水印文字
    /// </summary>
    /// <param name="sourcePicture">源图片文件</param>
    /// <param name="waterWords">需要添加到图片上的文字</param>
    /// <param name="alpha">透明度</param>
    /// <param name="position">位置</param>
    /// <param name="PicturePath">文件路径</param>
    /// <returns></returns>
    public string DrawWords(string sourcePicture,
                                      string waterWords,
                                      float alpha,
                                      ImagePosition position,
                                      string PicturePath)
    {
        //
        // 判断参数是否有效
        //
        if (sourcePicture == string.Empty || waterWords == string.Empty || alpha == 0.0 || PicturePath == string.Empty)
        {
            return sourcePicture;
        }

//
        // 源图片全路径
        //
        string sourcePictureName = PicturePath + sourcePicture;
        string fileExtension = System.IO.Path.GetExtension(sourcePictureName).ToLower();

//
        // 判断文件是否存在,以及文件名是否正确
        //
        if (System.IO.File.Exists(sourcePictureName) == false || (
            fileExtension != ".gif"  &&
            fileExtension != ".jpg" &&
            fileExtension != ".png" ))
        {
            return sourcePicture;
        }

//
        // 目标图片名称及全路径
        //
        string targetImage = sourcePictureName.Replace(System.IO.Path.GetExtension(sourcePictureName), "") + "_0703.jpg";

//创建一个图片对象用来装载要被添加水印的图片
        Image imgPhoto = Image.FromFile(sourcePictureName);

//获取图片的宽和高
        int phWidth = imgPhoto.Width;
        int phHeight = imgPhoto.Height;

//
        //建立一个bitmap,和我们需要加水印的图片一样大小
        Bitmap bmPhoto = new Bitmap(phWidth, phHeight, PixelFormat.Format24bppRgb);

//SetResolution:设置此 Bitmap 的分辨率
        //这里直接将我们需要添加水印的图片的分辨率赋给了bitmap
        bmPhoto.SetResolution(imgPhoto.HorizontalResolution, imgPhoto.VerticalResolution);

//Graphics:封装一个 GDI+ 绘图图面。
        Graphics grPhoto = Graphics.FromImage(bmPhoto);

//设置图形的品质
        grPhoto.SmoothingMode = SmoothingMode.AntiAlias;

//将我们要添加水印的图片按照原始大小描绘(复制)到图形中
        grPhoto.DrawImage(
         imgPhoto,                                           //   要添加水印的图片
         new Rectangle(0, 0, phWidth, phHeight), //  根据要添加的水印图片的宽和高
         0,                                                     //  X方向从0点开始描绘
         0,                                                     // Y方向
         phWidth,                                            //  X方向描绘长度
         phHeight,                                           //  Y方向描绘长度
         GraphicsUnit.Pixel);                              // 描绘的单位,这里用的是像素

//根据图片的大小我们来确定添加上去的文字的大小
        //在这里我们定义一个数组来确定
        int[] sizes = new int[] { 16, 14, 12, 10, 8, 6, 4 };

//字体
        Font crFont = null;
        //矩形的宽度和高度,SizeF有三个属性,分别为Height高,width宽,IsEmpty是否为空
        SizeF crSize = new SizeF();

//利用一个循环语句来选择我们要添加文字的型号
        //直到它的长度比图片的宽度小
        for (int i = 0; i < 7; i++)
        {
            crFont = new Font("arial", sizes[i], FontStyle.Bold);

//测量用指定的 Font 对象绘制并用指定的 StringFormat 对象格式化的指定字符串。
            crSize = grPhoto.MeasureString(waterWords, crFont);

// ushort 关键字表示一种整数数据类型
            if ((ushort)crSize.Width < (ushort)phWidth)
                break;
        }

//截边5%的距离,定义文字显示(由于不同的图片显示的高和宽不同,所以按百分比截取)
        int yPixlesFromBottom = (int)(phHeight * .05);

//定义在图片上文字的位置
        float wmHeight =  crSize.Height;
        float wmWidth = crSize .Width ;

float  xPosOfWm;
        float  yPosOfWm;

switch (position)
        {
            case ImagePosition .BottomMiddle :
                xPosOfWm = phWidth / 2 ;
                yPosOfWm = phHeight- wmHeight -10;
                break ;
            case ImagePosition .Center :
                xPosOfWm = phWidth / 2;
                yPosOfWm = phHeight / 2;
                break ;
            case ImagePosition .LeftBottom :
                xPosOfWm = wmWidth;
                yPosOfWm = phHeight - wmHeight - 10;
                break ;
            case ImagePosition .LeftTop :
                xPosOfWm = wmWidth/2 ;
                yPosOfWm = wmHeight / 2;
                break;
            case ImagePosition .RightTop :
                xPosOfWm = phWidth - wmWidth - 10;
                yPosOfWm = wmHeight;
                break ;
            case ImagePosition .RigthBottom :
                xPosOfWm = phWidth - wmWidth - 10;
                yPosOfWm = phHeight - wmHeight - 10;
                break ;
            case ImagePosition.TopMiddle :
                xPosOfWm = phWidth / 2;
                yPosOfWm = wmWidth;
                break ;
            default:
                xPosOfWm = wmWidth;
                yPosOfWm = phHeight - wmHeight - 10;
                break;
        }

//封装文本布局信息(如对齐、文字方向和 Tab 停靠位),显示操作(如省略号插入和国家标准 (National) 数字替换)和 OpenType 功能。
        StringFormat StrFormat = new StringFormat();

//定义需要印的文字居中对齐
        StrFormat.Alignment = StringAlignment.Center;

//SolidBrush:定义单色画笔。画笔用于填充图形形状,如矩形、椭圆、扇形、多边形和封闭路径。
        //这个画笔为描绘阴影的画笔,呈灰色
        int m_alpha = Convert .ToInt32 ( 256 * alpha);
        SolidBrush semiTransBrush2 = new SolidBrush(Color.FromArgb(m_alpha, 0, 0, 0));

//描绘文字信息,这个图层向右和向下偏移一个像素,表示阴影效果
        //DrawString 在指定矩形并且用指定的 Brush 和 Font 对象绘制指定的文本字符串。
        grPhoto.DrawString(waterWords,                                    //string of text
                                   crFont,                                         //font
                                   semiTransBrush2,                            //Brush
                                   new PointF(xPosOfWm + 1, yPosOfWm + 1),  //Position
                                   StrFormat);

//从四个 ARGB 分量(alpha、红色、绿色和蓝色)值创建 Color 结构,这里设置透明度为153
        //这个画笔为描绘正式文字的笔刷,呈白色
        SolidBrush semiTransBrush = new SolidBrush(Color.FromArgb(153, 255, 255, 255));

//第二次绘制这个图形,建立在第一次描绘的基础上
        grPhoto.DrawString(waterWords,                 //string of text
                                   crFont,                                   //font
                                   semiTransBrush,                           //Brush
                                   new PointF(xPosOfWm, yPosOfWm),  //Position
                                   StrFormat);

//imgPhoto是我们建立的用来装载最终图形的Image对象
        //bmPhoto是我们用来制作图形的容器,为Bitmap对象
        imgPhoto = bmPhoto;
        //释放资源,将定义的Graphics实例grPhoto释放,grPhoto功德圆满
        grPhoto.Dispose();

//将grPhoto保存
        imgPhoto.Save(targetImage, ImageFormat.Jpeg);
        imgPhoto.Dispose();

return targetImage.Replace(PicturePath, "");
    }
}

/// <summary>
/// 装载水印图片的相关信息
/// </summary>
public class WaterImage
{
    public WaterImage ()
    {

}

private string m_sourcePicture;
    /// <summary>
    /// 源图片地址名字(带后缀)
    /// </summary>
    public string SourcePicture
    {
        get { return m_sourcePicture; }
        set { m_sourcePicture = value; }
    }

private string  m_waterImager;
    /// <summary>
    /// 水印图片名字(带后缀)
    /// </summary>
    public string  WaterPicture
    {
        get { return m_waterImager; }
        set { m_waterImager = value; }
    }

private float  m_alpha;
    /// <summary>
    /// 水印图片文字的透明度
    /// </summary>
    public float  Alpha
    {
        get { return m_alpha; }
        set { m_alpha = value; }
    }

private ImagePosition  m_postition;
    /// <summary>
    /// 水印图片或文字在图片中的位置
    /// </summary>
    public ImagePosition  Position
    {
        get { return m_postition; }
        set { m_postition = value; }
    }

private string  m_words;
    /// <summary>
    /// 水印文字的内容
    /// </summary>
    public string  Words
    {
        get { return m_words; }
        set { m_words = value; }
    }
}

ps: 感谢清清月儿的文章。

转载于:https://www.cnblogs.com/edidu/archive/2007/07/23/828654.html

自己做的一个水印生成类相关推荐

  1. 写一个工具生成数据库实体类

    写一个java工具从数据库生成实体类 开始写之前的说明 ​ 这只是一个简单的解析数据库建表语句之后,根据解析结果生成java文件的一个简单工具.写的原因有两个. ​ 1:项目中没有人写实体类的注释,字 ...

  2. moba寻路_GitHub - ylmbtm/MoBaDemo: 用Unity做的一个类Moba游戏Demo

    游戏整体逻辑 出兵逻辑 游戏会在两个阵营的固定区域在固定的频率中出兵.小兵只能沿着规定好的轨道进行行走,并按照各个防守塔的顺序向敌方进行进攻. 需要注意的点 对于固定出现的小兵来说,不适合在其死亡之后 ...

  3. 第五十七篇:VS2015建立一个完整的c++工程:头文件.h 源文件.cpp,自动生成类

    之前没有用VS或者在vs中一个源程序写到底,没有使用C++编一个工程 打开VS2015 ,新建VS win32工程,前面步骤很简单,不再阐述 下面直接开始: 新建一个VC++ win32的程序, 在源 ...

  4. moba寻路_GitHub - aafasou/MoBaDemo: 用Unity做的一个类Moba游戏Demo

    游戏整体逻辑 出兵逻辑 游戏会在两个阵营的固定区域在固定的频率中出兵.小兵只能沿着规定好的轨道进行行走,并按照各个防守塔的顺序向敌方进行进攻. 需要注意的点 对于固定出现的小兵来说,不适合在其死亡之后 ...

  5. 开源一个优雅的类图生成工具

    前言 首先必须声明,这不是一篇广告或者标题党. 而是我开源了一个工具,可以优雅的为Java or Kotlin 项目生成 Class Diagram. 我推测列为读者会进来阅读,原因无非以下两点: 获 ...

  6. 《JAVA练习题目7》 定义一个素数生成器类PrimeGenerator,用于生成给定区间内的所有素数。(类PrimeGenerator都由类Main代替)

    题目内容: 定义一个素数生成器类PrimeGenerator,用于生成给定区间内的所有素数.要求PrimeGenerator类具有: 属性:start(区间起始值),end(区间终止值),两个属性均为 ...

  7. 一个封装了的ADO类,功能非常强大,并做了一个DEMO演示如何操作ACCESS数据库

    呵呵,说实在的,这个类俺是从外国论坛上下载的,自己研究了下如何使用并做了一个DEMO,演示如何操作ACCESS数据库.希望给那些没有接触数据库编程的初学者一点启发吧.有任何问题请及时与我联系^_^.程 ...

  8. C#做的一个加密/解密的类

    C#做的一个加密/解密的类 大家要有兴趣,可以一起来讨论一下 WebService数据交互安全问题,以下的这个代码,可以用于Dotnet环境下的任何托管方式的应用程序,在实际应用中有两个实例.其中,有 ...

  9. php生成饼状图 柱形图,求一个饼状图或柱状图php生成类或例子

    求一个饼状图或柱状图php生成类或例子 时间:2006/7/19 6:10:04 作者:佚名 人气:268 PHP代码:---------------------------------------- ...

最新文章

  1. 了解机器学习的八大专业术语
  2. 平流式隔油池计算_广州隔油池清理公司讲述清理隔油池程序和标准_新闻中心...
  3. “超级课程表”余佳文:初生牛犊不怕虎
  4. mysql定制化_【MySQL技巧】定制你的MySQL命令行-阿里云开发者社区
  5. JavaScript知识总结
  6. Winform中设置ZedGraph鼠标悬浮显示距离最近曲线上的点的坐标值和X轴与Y轴的标题
  7. Shell基础知识(三)
  8. 物理搬砖问题_搬砖姿势:风法
  9. JS 相等判断 / 类型判断
  10. Windows mobile美化之-短信界面美化修改~
  11. 535. TinyURL 的加密与解密
  12. 国外优秀的屏幕录象专家
  13. PHP7.2手册(最新版)
  14. 光圈,焦距,工作距离与景深之间的关系。
  15. Unity 时装换色
  16. 基于高德地图实现的公交线路查询功能
  17. 爬取京东商品信息出现登陆页面的解决
  18. 记一次糟糕的驾照学习经历
  19. ubuntu 下如何下载linux内核源码
  20. Mybatis开启一级、二级缓存

热门文章

  1. 岭回归、LASSO与LAR的几何意义
  2. vim 批量替换字符串_vim批量替换,效率就是高。附常用批量替换命令
  3. 双目测距(一)--图像获取与单目标定
  4. W Zong / A Robust Open-source Algorithm to Detect Onset and Duration of QRS Complexes
  5. osea/Beat Classification 4.3-4.5
  6. mysql 下载教程_MySQL下载安装详情图文教程
  7. phpstud使用教程_phpStudy2017使用教程,自定义php版本功能上线!
  8. 【数据库原理实验(openGauss)】事务与并发控制
  9. git pull命令报错
  10. sbt命令行常用命令