/// <summary> 
        /// WaterMark 的摘要说明 
        /// </summary> 
        /// 图片加水印
        /// <param name="strCopyright">要加入的文字 </param>
        /// <param name="strMarkPath">水印图片路径 </param>
        /// <param name="strPhotoPath">要加水印的图片路径 </param>
        /// <param name="strSavePath">处理后的图片路径 </param>
        /// <param name="iMarkRightSpace">水印在修改图片中距右边的宽度 </param>
        /// <param name="iMarkButtomSpace">水印在修改图片中距底部的高度 </param>
        /// <param name="iDiaphaneity">水印图片的透明度 </param>
        /// <param name="iFontRightSpace">文字 </param>
        /// <param name="iFontButtomSpace">文字 </param>
        /// <param name="iFontDiaphaneity">文字 </param>
        /// <param name="bShowCopyright">是否显示文字 </param>
        /// <param name="bShowMarkImage">是否显示水印图片 </param>
        public class WaterMark
        {
            #region  param

private string strCopyright, strMarkPath, strPhotoPath, strSavePath;
            private int iMarkRightSpace, iMarkButtomSpace, iDiaphaneity;
            private int iFontRightSpace = 0, iFontButtomSpace = 0, iFontDiaphaneity = 80;
            private int iFontSize = 10;
            private bool bShowCopyright = true, bShowMarkImage = true;

#endregion

#region WaterMark

public WaterMark()
            {
                this.strCopyright = "";
                this.strMarkPath = null;
                this.strPhotoPath = null;
                this.strSavePath = null;
                this.iDiaphaneity = 70;
                this.iMarkRightSpace = 0;
                this.iMarkButtomSpace = 0;
            }

/// <summary> 
            /// 主要用两样都加的
            /// </summary> 
            public WaterMark(string copyright, string markPath, string photoPath, string savePath)
            {
                this.strCopyright = copyright;
                this.strMarkPath = markPath;
                this.strPhotoPath = photoPath;
                this.strSavePath = savePath;
                this.iDiaphaneity = 70;
                this.iMarkRightSpace = 0;
                this.iMarkButtomSpace = 0;
            }

#endregion

#region property

/// <summary> 
            /// 设置是否显示水印文字
            /// </summary> 
            public bool ShowCopyright
            {
                set { this.bShowCopyright = value; }
            }

/// <summary> 
            /// 设置是否显示水印图片
            /// </summary> 
            public bool ShowMarkImage
            {
                set { this.bShowMarkImage = value; }
            }
            /// <summary> 
            /// 获取或设置要加入的文字
            /// </summary> 
            public string Copyright
            {
                set { this.strCopyright = value; }
            }

/// <summary> 
            /// 获取或设置加水印后的图片路径
            /// </summary> 
            public string SavePath
            {
                get { return this.strSavePath; }
                set { this.strSavePath = value; }
            }

/// <summary> 
            /// 获取或设置水印图片路径
            /// </summary> 
            public string MarkPath
            {
                get { return this.strMarkPath; }
                set { this.strMarkPath = value; }
            }

/// <summary> 
            /// 获取或设置要加水印图片的路径
            /// </summary> 
            public string PhotoPath
            {
                get { return this.strPhotoPath; }
                set { this.strPhotoPath = value; }
            }

/// <summary> 
            /// 设置水印图片的透明度
            /// </summary> 
            public int Diaphaneity
            {
                set
                {
                    if (value > 0 && value <= 100)
                        this.iDiaphaneity = value;
                }
            }

/// <summary> 
            /// 设置水印字体的透明度0-255
            /// </summary> 
            public int FontDiaphaneity
            {
                set
                {
                    if (value >= 0 && value <= 255)
                        this.iFontDiaphaneity = value;
                }
            }

/// <summary> 
            /// 设置水印图片在修改图片中距左边的高度
            /// </summary> 
            public int MarkRightSpace
            {
                set { this.iMarkRightSpace = value; }
            }

/// <summary> 
            /// 设置水印图片在修改图片中距底部的高度
            /// </summary> 
            public int MarkButtomSpace
            {
                set { this.iMarkButtomSpace = value; }
            }

/// <summary> 
            /// 设置水印字体在修改图片中距左边的距离
            /// </summary> 
            public int FontRightSpace
            {
                set { iFontRightSpace = value; }
            }

/// <summary> 
            /// 设置水印字体在修改图片中距底部的高度
            /// </summary> 
            public int FontButtomSpace
            {
                set { iFontButtomSpace = value; }
            }

#endregion

/// <summary> 
            /// 生成水印图片
            /// </summary> 
            /// <returns> </returns> 
            public void createMarkPhoto()
            {
                Bitmap bmWatermark = null;
                FileStream fileStream = new FileStream(this.strPhotoPath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
                Image gPhoto = Image.FromStream(fileStream);
                int PhotoWidth = gPhoto.Width;
                int PhotoHeight = gPhoto.Height;
                //Bitmap bitPhoto = new Bitmap(PhotoWidth, PhotoHeight, PixelFormat.Format24bppRgb);
                Bitmap bitPhoto = new Bitmap(PhotoWidth, PhotoHeight);
                bitPhoto.SetResolution(gPhoto.HorizontalResolution, gPhoto.VerticalResolution);

try
                {
                    if (bShowCopyright)
                    {
                        Graphics grPhoto = Graphics.FromImage(bitPhoto);
                        //grPhoto.SmoothingMode = SmoothingMode.AntiAlias;//这句代码的作用是让合成模式为自动反锯齿,也就是所谓的"模糊"
                        grPhoto.DrawImage(gPhoto, new Rectangle(0, 0, PhotoWidth, PhotoHeight), 0, 0, PhotoWidth, PhotoHeight, GraphicsUnit.Pixel);

Font crFont = new Font("楷体", iFontSize, FontStyle.Bold);
                        SizeF crSize = grPhoto.MeasureString(strCopyright, crFont);

//设置字体在图片中的位置
                        float yPosFromBottom = PhotoHeight - iFontButtomSpace - (crSize.Height);

//float xCenterOfImg = (phWidth/2);
                        float xCenterOfImg = PhotoWidth - iFontRightSpace - (crSize.Width / 2);
                        //设置字体居中

StringFormat StrFormat = new StringFormat();
                        StrFormat.Alignment = StringAlignment.Center;

//设置绘制文本的颜色和纹理 (Alpha=153)
                        SolidBrush semiTransBrush2 = new SolidBrush(Color.FromArgb(this.iFontDiaphaneity, 0, 0, 0));

//将版权信息绘制到图象上
                        grPhoto.DrawString(strCopyright, crFont, semiTransBrush2, new PointF(xCenterOfImg, yPosFromBottom), StrFormat);

gPhoto = bitPhoto;
                        grPhoto.Dispose();
                    }

if (bShowMarkImage)
                    {
                        //创建一个需要填充水银的Image对象
                        Image imgWatermark = new Bitmap(strMarkPath);
                        int iMarkWidth = imgWatermark.Width;
                        int iMarkmHeight = imgWatermark.Height;

Graphics grWatermark = null;
                        if (bShowCopyright)
                        {
                            //在原来修改过的bmPhoto上创建一个水银位图
                            bmWatermark = new Bitmap(bitPhoto);
                            bmWatermark.SetResolution(gPhoto.HorizontalResolution, gPhoto.VerticalResolution);
                        }
                        else
                        {
                            bmWatermark = new Bitmap(gPhoto);
                        }

//将位图bmWatermark加载到Graphics对象
                        grWatermark = Graphics.FromImage(bmWatermark);
                        ImageAttributes imageAttributes = new ImageAttributes();

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},
                        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, (float)iDiaphaneity/100f, 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);
                        grWatermark.DrawImage(imgWatermark, new Rectangle((PhotoWidth - iMarkRightSpace - (iMarkWidth / 2)), (PhotoHeight - iMarkButtomSpace - (iMarkmHeight / 2)), iMarkWidth, iMarkmHeight), 0, 0, iMarkWidth, iMarkmHeight, GraphicsUnit.Pixel, imageAttributes);

gPhoto = bmWatermark;
                        grWatermark.Dispose();
                        imgWatermark.Dispose();
                    }
                    //设置输出图片质量,默认输出是60%的清晰度
                    EncoderParameter ep = new EncoderParameter(Encoder.Quality, 100L);
                    EncoderParameters eps = new EncoderParameters(1);
                    eps.Param[0] = ep;
                    ImageCodecInfo ic = GetCodecInfo("image/jpeg");
                    fileStream.Close();
                    gPhoto.Save(strSavePath, ic, eps);
                    bmWatermark.Dispose();
                    bitPhoto.Dispose();
                    gPhoto.Dispose();
                    ep.Dispose();
                    eps.Dispose();
                }
                finally
                {
                    if (bitPhoto != null)
                        bitPhoto.Dispose();
                    if (bmWatermark != null)
                        bmWatermark.Dispose();
                    fileStream.Close();
                    gPhoto.Dispose();
                }
            }

private ImageCodecInfo GetCodecInfo(String mimeType)
            {
                ImageCodecInfo[] CodecInfo = ImageCodecInfo.GetImageEncoders();
                foreach (ImageCodecInfo ici in CodecInfo)
                {
                    if (ici.MimeType == mimeType) return ici;
                }
                return null;
            }
        }

C#给图片加水印文字或图片相关推荐

  1. JAVA实现图片加水印及Base64图片字符串加水印

    JAVA实现图片加水印及Base64图片字符串加水印 可将根据不同需求选择场景一,场景二来实现家水印操作,个人感觉比我上一遍文章<base64字符串加水印 >实在 场景一: 涉及上送过来的 ...

  2. 火车头dede采集接口,图片加水印,远程图片本地化,远程无后缀的无图片本地化...

    <?php /*[LocoySpider] (C)2005-2010 Lewell Inc.火车采集器 DedeCMS 5.7 UTF8 文章发布接口 Update content: 图片加水印 ...

  3. java图片加气泡文字,动态图片加气泡文字 微信动态图片加文字教程

    现在微信聊天少不了表情图片,这越有个性.独特的就越受大众喜爱,每每看到好看的图片时,大家也会忍不住收藏起来用.那你有没有想过自己P图呢?学一下PS图片创作一些独一无二的搞笑表情图,放在微信上秀秀吧,也 ...

  4. java图片加气泡文字_图片加气泡文字

    现在微信聊天少不了表情图片,这越有个性.独特的就越受大众喜爱,每每看到好看的图片时,大家也会忍不住收藏起来用.那你有没有想过自己P图呢?学一下PS图片创作一些独一无二的搞笑表情图,放在微信上秀秀吧,也 ...

  5. AS3给图片加水印文字

    <需要flash player 10+支持> 通过FileReference获取选中的图片,调用它的load方法,将图片存入到内存中.使用Loader(flash.display.Load ...

  6. php 图片加水印文字水印

    /*给图片加文字水印的方法*/ $dst_path = 'http://f4.topitme.com/4/15/11/1166351597fe111154l.jpg';//保证路径正确 $dst = ...

  7. android图片加水印,文字

    两种方法:1.直接在图片上写文字String str = "PICC要写的文字";ImageView image = (ImageView) this.findViewById(R ...

  8. java给图片加水印_Java 为图片添加水印(图片水印,文字水印)

    Java 为图片添加水印(图片水印,文字水印) 2014-07-28·WeaponX 5267 次浏览 ```java import java.awt.Color; import java.awt.F ...

  9. html 图片加水印,js给图片加水印的两种方法

    第一种(较复杂) 根据图片路径获取图片数据,转成blob类型 用FileReader读取图片blob数据为dataURL 创建img标签,src属性为dataURL 监听img.onload, 创建c ...

  10. html2canvas给图片添加水印,小程序用Canvas给图片加水印,拼接图片,制作名片

    Canvas是微信小程序中的一个原生组件,因此我们在使用它的时候要特别注意微信小程序对原生组件的使用说明.canvas这个组件其实就是一个画布,你可以在上面画很多你用其他方式不好实现的内容.下面我就将 ...

最新文章

  1. PPPoE***2:PADR耗竭sessionid
  2. (十二)springmvc+mybatis+dubbo+zookeeper分布式架构 整合 - zookeeper注册中心安装
  3. 学习jvm,关于MAT an internal error occurred during:Parsing heap dump from问题
  4. 论文排版怕翻车?这个排版神器是时候用起来了!
  5. 【CodeForces - 144C】Anagram Search(尺取,滑窗问题,处理字符串计数)
  6. SpringBoot 自带工具类~FileCopyUtils
  7. python输入变量输出常量_Python输入input、输出print
  8. 前端走向后台,node.js基本
  9. 移动互联网消息推送原理:长连接+心跳机制(MQTT协议)
  10. resiprocate 之repro注册
  11. java题目:有n个整数,使其前面各数顺序向后移m个位置,最后m个数变成最前面的m个数
  12. linux 图形验证码不显示不出来的,linux下图形验证码显示问题
  13. 19.数学与经济管理
  14. h5页面自定义字体_自定义app字体(原生+h5)
  15. 三国演义人物出场次数统计
  16. 小鸟云服务器上线了新的操作系统
  17. 将精确到时分秒的日期截取到年月日
  18. python破解网易反爬虫机制
  19. vue vuex和pinia(菠萝)的区别
  20. 从零开始学_JavaScript_系列(六)——CSS的padding、margin、border属性超详细解释(图文)

热门文章

  1. SQL语句查询出现异常,SQL语句:*** 给定关键字不在字典中。
  2. 用html计算长方形的面积公式,长方形的面积公式
  3. 如何使用Python 绘制惊艳众人的瀑布图
  4. HTML/CSS class6 table布局
  5. 极客也可以很亲民,酷炫设计、多变造型的华为智能眼镜即将发布
  6. Android 手写和笔锋研究资料
  7. c语言中srand的作用,C++随机数(rand和srand)函数用法详解
  8. 耐得住寂寞,才能守得住繁华
  9. Mac自带apache2搭建服务请求localhost报 403 Forbidden
  10. 人工智能对医疗和健康产业的冲击和革命——意识上传技术展望