逆天通用水印支持Winform,WPF,Web,WP,Win10。支持位置选择(9个位置 ==》[X])

好几天没上QQ了,今天上了个QQ,,额....额...貌似消息还挺多,没及时回复的还请见谅~~刚好昨天无聊把水印这快封装出来了,支持图片水印,文字水印,索引图水印和非索引图水印,支持位置选择(9个位置 ==》[X])。好吧不高兴屁话了,软件是WPF的(只满足我的需求,如果有需要可以出第个二版本),水印核心代码封装了个Helper类(通用:支持Winform,WPF,Web,WP,Win10)
软件共享一下==>http://pan.baidu.com/s/1qWDwv4C 
源码 http://pan.baidu.com/s/1gd1300z

水印类

/// <summary>
/// 水印类(重要参数:原图类型,水印类型,水印位置,图片水印路径,文字水印内容)
/// </summary>
public class WaterMark
{
    #region 基础属性设置
    private string _photoType;
    /// <summary>
    /// 原图类型
    /// </summary>
    public string PhotoType
    {
        get { return _photoType; }
        set { _photoType = value; }
    }

    private WaterMarkTypeEnum _waterMarkType = WaterMarkTypeEnum.NoneMark;
    /// <summary>
    /// 水印类型
    /// </summary>
    public WaterMarkTypeEnum WaterMarkType
    {
        get { return _waterMarkType; }
        set { _waterMarkType = value; }
    }
    #endregion

    #region 水印效果设置
    private WaterMarkLocationEnum _waterMarkLocation = WaterMarkLocationEnum.BottomRight;
    /// <summary>
    /// 水印位置
    /// </summary>
    public WaterMarkLocationEnum WaterMarkLocation
    {
        get { return _waterMarkLocation; }
        set { _waterMarkLocation = value; }
    }

    private float _transparency = 0.7f;
    /// <summary>
    /// 水印透明度
    /// </summary>
    public float Transparency
    {
        get { return _transparency; }
        set { _transparency = value; }
    }
    #endregion

    #region 图片水印设置
    private string _imgPath;
    /// <summary>
    /// 图片水印路径
    /// </summary>
    public string ImgPath
    {
        get { return _imgPath; }
        set { _imgPath = value; }
    }
    #endregion

    #region 文字水印设置
    private string _text = "dunitian";
    /// <summary>
    /// 文字水印内容
    /// </summary>
    public string Text
    {
        get { return _text; }
        set { _text = value; }
    }

    private string _fontFamily = "微软雅黑";
    /// <summary>
    /// 文字字体
    /// </summary>
    public string FontFamily
    {
        get { return _fontFamily; }
        set { _fontFamily = value; }
    }

    private Brush _brushesColor = Brushes.Black;
    /// <summary>
    /// 文字颜色
    /// </summary>
    public Brush BrushesColor
    {
        get { return _brushesColor; }
        set { _brushesColor = value; }
    }

    private  FontStyle _fontStyle = FontStyle.Regular;
    /// <summary>
    /// 字体样式
    /// </summary>
    public FontStyle FontStyle
    {
        get { return _fontStyle; }
        set { _fontStyle = value; }
    }

    private float _fontSize = 14f;
    /// <summary>
    /// 字体大小
    /// </summary>
    public float FontSize
    {
        get { return _fontSize; }
        set { _fontSize = value; }
    }
    #endregion
}

 水印位置枚举

/// <summary>
/// 水印位子
/// </summary>
public enum WaterMarkLocationEnum
{
    /// <summary>
    /// 顶部居左
    /// </summary>
    TopLeft = 1,
    /// <summary>
    /// 顶部居中
    /// </summary>
    TopCenter = 2,
    /// <summary>
    /// 顶部居右
    /// </summary>
    TopRight = 3,
    /// <summary>
    /// 中部居左
    /// </summary>
    CenterLeft = 4,
    /// <summary>
    /// 中部居中
    /// </summary>
    CenterCenter = 5,
    /// <summary>
    /// 中部居右
    /// </summary>
    CenterRight = 6,
    /// <summary>
    /// 底部居左
    /// </summary>
    BottomLeft = 7,
    /// <summary>
    /// 底部居左
    /// </summary>
    BottomCenter = 8,
    /// <summary>
    /// 底部居左
    /// </summary>
    BottomRight = 9,
}

 水印类型枚举

/// <summary>
    /// 水印类型
    /// </summary>
    public enum WaterMarkTypeEnum
    {
        /// <summary>
        /// 文字
        /// </summary>
        Text = 1,
        /// <summary>
        /// 图片
        /// </summary>
        Image = 2,
        /// <summary>
        /// 无水印
        /// </summary>
        NoneMark = 3
    }

 水印帮助类(自己封装的,有bug可以像我反馈)

/// <summary>
/// 水印帮助类
/// </summary>
public class WaterMarkHelper
{
    #region 设置水印
    /// <summary>
    /// 设置水印
    /// </summary>
    /// <param name="imgPath"></param>
    /// <param name="model"></param>
    /// <returns></returns>
    public static Image SetWaterMark(string imgPath, WaterMark model)
    {
        //获取背景图
        Image imgSource = Image.FromFile(imgPath);
        //获取水印图片
        Image markImg = null;

        //水印检验(文字,图片[路径下是否存在图片])
        #region 水印校验+水印处理
        if (model == null)
        {
            return null;
        }
        //看看原图是否存在
        if (!System.IO.File.Exists(imgPath))
        {
            return null;
        }
        //根据水印类型校验+水印处理
        switch (model.WaterMarkType)
        {
            case WaterMarkAPP.Enums.WaterMarkTypeEnum.Text:
                if (string.IsNullOrEmpty(model.Text))
                {
                    return null;
                }
                else
                {
                    markImg = TextToImager(model);//水印处理-如果是文字就转换成图片
                }
                break;
            case WaterMarkAPP.Enums.WaterMarkTypeEnum.Image:
                if (!System.IO.File.Exists(model.ImgPath))
                {
                    return null;
                }
                else
                {
                    //获得水印图像          
                    markImg = Image.FromFile(model.ImgPath);
                }
                break;
            case WaterMarkAPP.Enums.WaterMarkTypeEnum.NoneMark:
                return imgSource;
        }
        #endregion

        #region 创建颜色矩阵
        //创建颜色矩阵
        float[][] ptsArray ={
                             new float[] {1, 0, 0, 0, 0},
                             new float[] {0, 1, 0, 0, 0},
                             new float[] {0, 0, 1, 0, 0},
                             new float[] {0, 0, 0, model.Transparency, 0}, //注意:0.0f为完全透明,1.0f为完全不透明
                             new float[] {0, 0, 0, 0, 1}};
        ColorMatrix colorMatrix = new ColorMatrix(ptsArray);
        //新建一个Image属性
        ImageAttributes imageAttributes = new ImageAttributes();
        //将颜色矩阵添加到属性
        imageAttributes.SetColorMatrix(colorMatrix, ColorMatrixFlag.Default, ColorAdjustType.Default);
        #endregion

        //原图格式检验+水印
        #region 原图格式检验+水印

        //判断是否是索引图像格式
        if (imgSource.PixelFormat == PixelFormat.Format1bppIndexed || imgSource.PixelFormat == PixelFormat.Format4bppIndexed || imgSource.PixelFormat == PixelFormat.Format8bppIndexed)
        {
            #region 索引图片,转成位图再加图片
            //转成位图,这步很重要
            Bitmap bitmap = new Bitmap(imgSource.Width, imgSource.Height);
            Graphics graphic = Graphics.FromImage(bitmap);

            #region 缩放处理
            //如果原图小于水印图片 等比缩放水印图
            if (markImg.Width >= imgSource.Width || markImg.Height >= imgSource.Height)
            {
                markImg = ImageShrink(imgSource, markImg);
            }
            #endregion

            #region 水印位置
            //水印位置
            int x;
            int y;
            WaterMarkLocations(model, imgSource, markImg, out x, out y);
            #endregion

            //将原图画在位图上
            graphic.DrawImage(imgSource, new Point(0, 0));

            //将水印加在位图上
            graphic.DrawImage(markImg, new Rectangle(x, y, markImg.Width, markImg.Height), 0, 0, markImg.Width, markImg.Height, GraphicsUnit.Pixel, imageAttributes);

            graphic.Dispose();
            return bitmap;
            #endregion
        }
        else
        {
            #region 非索引图片,直接在上面加上水印
            Graphics graphic = Graphics.FromImage(imgSource);

            #region 缩放处理
            //如果原图小于水印图片 等比缩放水印图
            if (markImg.Width >= imgSource.Width || markImg.Height >= imgSource.Height)
            {
                markImg = ImageShrink(imgSource, markImg);
            }
            #endregion

            #region 水印位置
            //水印位置
            int x;
            int y;
            WaterMarkLocations(model, imgSource, markImg, out x, out y);
            #endregion

            //将水印加在原图上
            graphic.DrawImage(markImg, new Rectangle(x, y, markImg.Width, markImg.Height), 0, 0, markImg.Width, markImg.Height, GraphicsUnit.Pixel, imageAttributes);

            graphic.Dispose();
            return imgSource;
            #endregion
        }
        #endregion
    }
    #endregion

    #region 水印处理-文字转图片
    /// <summary>
    /// 水印处理-文字转图片
    /// </summary>
    /// <param name="model"></param>
    /// <returns></returns>
    private static Image TextToImager(WaterMark model)
    {
        Font f = new Font(model.FontFamily, model.FontSize, model.FontStyle);
        Bitmap fbitmap = new Bitmap(Encoding.GetEncoding("GBK").GetByteCount(model.Text) / 2 * f.Height, f.Height);
        Graphics gh = Graphics.FromImage(fbitmap);//创建一个画板;
        gh.SmoothingMode = SmoothingMode.AntiAlias;
        gh.DrawString(model.Text, f, model.BrushesColor, 0, 0);//画字符串
        return fbitmap as Image;
    }
    #endregion

    #region 水印位置
    /// <summary>
    /// 水印位置
    /// </summary>
    /// <param name="model"></param>
    /// <param name="imgSource"></param>
    /// <param name="markImg"></param>
    /// <param name="x"></param>
    /// <param name="y"></param>
    private static void WaterMarkLocations(WaterMark model, Image imgSource, Image markImg, out int x, out int y)
    {
        x = 0;
        y = 0;
        switch (model.WaterMarkLocation)
        {
            case WaterMarkLocationEnum.TopLeft:
                x = 0;
                y = 0;
                break;
            case WaterMarkLocationEnum.TopCenter:
                x = imgSource.Width / 2 - markImg.Width / 2;
                y = 0;
                break;
            case WaterMarkLocationEnum.TopRight:
                x = imgSource.Width - markImg.Width;
                y = 0;
                break;
            case WaterMarkLocationEnum.CenterLeft:
                x = 0;
                y = imgSource.Height / 2 - markImg.Height / 2;
                break;
            case WaterMarkLocationEnum.CenterCenter:
                x = imgSource.Width / 2 - markImg.Width / 2;
                y = imgSource.Height / 2 - markImg.Height / 2;
                break;
            case WaterMarkLocationEnum.CenterRight:
                x = imgSource.Width - markImg.Width;
                y = imgSource.Height / 2 - markImg.Height / 2;
                break;
            case WaterMarkLocationEnum.BottomLeft:
                x = 0;
                y = imgSource.Height - markImg.Height;
                break;
            case WaterMarkLocationEnum.BottomCenter:
                x = imgSource.Width / 2 - markImg.Width / 2;
                y = imgSource.Height - markImg.Height;
                break;
            case WaterMarkLocationEnum.BottomRight:
                x = imgSource.Width - markImg.Width;
                y = imgSource.Height - markImg.Height;
                break;
        }
    }
    #endregion

    #region 缩放水印
    /// <summary>
    /// 等比缩放水印图(缩小到原图的1/3)
    /// </summary>
    /// <param name="imgSource"></param>
    /// <param name="successImage"></param>
    /// <returns></returns>
    private static Image ImageShrink(Image imgSource, Image markImg)
    {
        int w = 0;
        int h = 0;

        Image.GetThumbnailImageAbort callb = null;

        //对水印图片生成缩略图,缩小到原图的1/3(以短的一边为准)                    
        if (imgSource.Width < imgSource.Height)
        {
            w = imgSource.Width / 3;
            h = markImg.Height * w / markImg.Width;
        }
        else
        {
            h = imgSource.Height / 3;
            w = markImg.Width * h / markImg.Height;
        }
        markImg = markImg.GetThumbnailImage(w, h, callb, new System.IntPtr());
        return markImg;
    }
    #endregion
}

  简单调用:

文字水印调用:

/// <summary>
/// 文字水印调用
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Button_Click(object sender, RoutedEventArgs e)
{
    //给水印对象赋对应的值
    WaterMark waterMark = new WaterMark();
    waterMark.WaterMarkType = Enums.WaterMarkTypeEnum.Text;
    waterMark.Transparency = 0.7f;
    waterMark.Text = "dunitian.cnblogs.com";
    waterMark.FontStyle = System.Drawing.FontStyle.Bold;
    waterMark.FontFamily = "Consolas";
    waterMark.FontSize = 20f;
    waterMark.BrushesColor = System.Drawing.Brushes.YellowGreen;
    waterMark.WaterMarkLocation = Enums.WaterMarkLocationEnum.CenterCenter;

    //调用
    Image successImage = WaterMarkHelper.SetWaterMark("text.png", waterMark);
    //保存
    successImage.Save("text1.png", System.Drawing.Imaging.ImageFormat.Png);

    MessageBox.Show("请查看软件根目录", "成功");
}

  图片水印调用:

/// <summary>
        /// 图片水印调用
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            //给水印对象赋对应的值
            WaterMark waterMark = new WaterMark();
            waterMark.WaterMarkType = Enums.WaterMarkTypeEnum.Image;
            waterMark.ImgPath = "水印.png";
            waterMark.WaterMarkLocation = Enums.WaterMarkLocationEnum.CenterCenter;
            waterMark.Transparency = 0.7f;

            //调用
            Image successImage = WaterMarkHelper.SetWaterMark("text.png", waterMark);
            //保存
            successImage.Save("text2.png", System.Drawing.Imaging.ImageFormat.Png);

            MessageBox.Show("请查看软件根目录","成功");
        }

  软件源码:(helper类不是最新,用上文的helper类)

 (调用部分我做了一个通用apidemo)

效果:

 源码下载:http://pan.baidu.com/s/1gd1300z

原文发布于微信公众号 - 我为Net狂(dotNetCrazy)

原文发表时间:2015-11-06

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

发表于

我来说两句

0 条评论
登录 后参与评论

相关文章

来自专栏知道一点点

【原创】bootstrap框架的学习 第五课

<h2>引导主体副本</h2> <p class="lead">这是一个演示引导主体副本用法的实例。</p>

9230
来自专栏前端之路

–探索CSS3动画、过渡

15250
来自专栏Android干货

CSS基本知识(慕课网)

40360
来自专栏前端小叙

js焦点轮播图

汇集网上焦点轮播图的实现方式,自己试了下,不过鼠标悬浮停止动画和鼠标离开动画播放好像没生效,不太明白,最后两行代码中,为什么可以直接写stop和play。不用加...

744130
来自专栏上善若水

018android初级篇之自定义圆形进度条

项目需要,需要一个圆形的进度条,所想到的实现方案是这样的: 自定义View,订制一个圆形的进度条。下面简述实现,有不当之处敬请指正。

13150
来自专栏进击的君君的前端之路

CSS常见样式(一)

31230
来自专栏Nian糕的私人厨房

CSS 基础

层叠样式表 (Cascading Style Sheets),一种用来为结构化文档(如 HTML 文档或 XML 应用)添加样式(字体、间距和颜色等)的计算机语...

13140
来自专栏我和未来有约会

Blend基础-布局控件

布局 什么是布局? Panels控件(其实就是容器控件) 对内部的子控件提供了自动布局功能 可以在容器控件内继续添加容器控件(一个复杂的界面往往是多种容器控件...

24860
来自专栏cnblogs

BOOtstrap源码分析之 tooltip、popover

一、tooltip(提示框) 源码文件: Tooltip.js Tooltip.scss 实现原理: 1、获取当前要显示tooltip的元素的定位信息(top...

33060
来自专栏Android常用基础

自定义View(九)-View的工作原理- View的layout()和draw()

上一节我们将View的测量流程理的差不多了,这篇我们来看下View的剩下的2大流程layout(布局)和draw(绘制)。相对测量来说,布局与绘制就简单了许多,...

12820

扫码关注云+社区

领取腾讯云代金券