单个图片、批量图片加入自己定义水印内容小程序开发
图1 单个图片加入水印
图2 批量图片加入水印 凝视:当中的http://blog.csdn.net/chr23899 版权全部 转载请说明 即为加入的自己定义水印内容。
主要水印代码实现例如以下:
public void BuildWatermark(string rSrcImgPath, string rMarkImgPath, string rMarkText, string rDstImgPath, int locatesize) { //下面(代码)从一个指定文件创建了一个Image 对象。然后为它的 Width 和 Height定义变量。 //这些长度待会被用来建立一个以24 bits 每像素的格式作为颜色数据的Bitmap对象。 Image imgPhoto = Image.FromFile(rSrcImgPath); int phWidth = imgPhoto.Width; int phHeight = imgPhoto.Height; Bitmap bmPhoto = new Bitmap(phWidth, phHeight, PixelFormat.Format24bppRgb); bmPhoto.SetResolution(72, 72); Graphics grPhoto = Graphics.FromImage(bmPhoto); //这个代码加载水印图片,水印图片已经被保存为一个BMP文件。以绿色(A=0,R=0,G=255,B=0)作为背景颜色。说明:当中的locatesize为1在下方 2在中间 7在上面;//再一次,会为它的Width 和Height定义一个变量。
Image imgWatermark = new Bitmap(rMarkImgPath); int wmWidth = imgWatermark.Width; int wmHeight = imgWatermark.Height; //这个代码以100%它的原始大小绘制imgPhoto 到Graphics 对象的(x=0,y=0)位置。 //以后全部的画图都将发生在原来照片的顶部。
grPhoto.SmoothingMode = SmoothingMode.AntiAlias; grPhoto.DrawImage( imgPhoto, new Rectangle(0, 0, phWidth, phHeight), 0, 0, phWidth, phHeight, GraphicsUnit.Pixel); //为了最大化版权信息的大小,我们将測试7种不同的字体大小来决定我们能为我们的照片宽度使用的可能的最大大小。 //为了有效地完毕这个,我们将定义一个整型数组,接着遍历这些整型值測量不同大小的版权字符串。 //一旦我们决定了可能的最大大小,我们就退出循环,绘制文本 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++) { crFont = new Font("arial", sizes[i], FontStyle.Bold); crSize = grPhoto.MeasureString(rMarkText, crFont); if ((ushort)crSize.Width < (ushort)phWidth) break; } //由于全部的照片都有各种各样的高度。所以就决定了从图象底部開始的5%的位置開始。
//使用rMarkText字符串的高度来决定绘制字符串合适的Y坐标轴。 //通过计算图像的中心来决定X轴,然后定义一个StringFormat 对象,设置StringAlignment 为Center。 int yPixlesFromBottom = (int)(phHeight * .05); float yPosFromBottom = ((phHeight / locatesize - yPixlesFromBottom) - (crSize.Height / 2)); float xCenterOfImg = (phWidth / 2); StringFormat StrFormat = new StringFormat(); StrFormat.Alignment = StringAlignment.Center; //如今我们已经有了全部所需的位置坐标来使用60%黑色的一个Color(alpha值153)创建一个SolidBrush 。 //在偏离右边1像素。底部1像素的合适位置绘制版权字符串。
//这段偏离将用来创建阴影效果。使用Brush反复这样一个过程。在前一个绘制的文本顶部绘制相同的文本。 SolidBrush semiTransBrush2 = new SolidBrush(Color.FromArgb(153, 0, 0, 0)); grPhoto.DrawString(rMarkText, crFont, semiTransBrush2, new PointF(xCenterOfImg + 1, yPosFromBottom + 1), StrFormat); SolidBrush semiTransBrush = new SolidBrush( Color.FromArgb(153, 255, 255, 255)); grPhoto.DrawString(rMarkText, crFont, semiTransBrush, new PointF(xCenterOfImg, yPosFromBottom), StrFormat); //依据前面改动后的照片创建一个Bitmap。
把这个Bitmap加载到一个新的Graphic对象。 Bitmap bmWatermark = new Bitmap(bmPhoto); bmWatermark.SetResolution( imgPhoto.HorizontalResolution, imgPhoto.VerticalResolution); Graphics grWatermark = Graphics.FromImage(bmWatermark); //通过定义一个ImageAttributes 对象并设置它的两个属性,我们就是实现了两个颜色的处理,以达到半透明的水印效果。
//处理水印图象的第一步是把背景图案变为透明的(Alpha=0, R=0, G=0, B=0)。我们使用一个Colormap 和定义一个RemapTable来做这个。 //就像前面展示的,我的水印被定义为100%绿色背景,我们将搜到这个颜色,然后代替为透明。
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 }; //第二个颜色处理用来改变水印的不透明性。
//通过应用包括提供了坐标的RGBA空间的5x5矩阵来做这个。
//通过设定第三行、第三列为0.3f我们就达到了一个不透明的水平。结果是水印会轻微地显示在图象底下一些。 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, 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); //随着两个颜色处理增加到imageAttributes 对象。我们如今就能在照片右手边上绘制水印了。
//我们会偏离10像素究竟部。10像素到左边。 int markWidth; int markHeight; //mark比原来的图宽 if (phWidth <= wmWidth) { markWidth = phWidth - 10; markHeight = (markWidth * wmHeight) / wmWidth; } else if (phHeight <= wmHeight) { markHeight = phHeight - 10; markWidth = (markHeight * wmWidth) / wmHeight; } else { markWidth = wmWidth; markHeight = wmHeight; } int xPosOfWm = ((phWidth - markWidth) - 10); int yPosOfWm = 10; grWatermark.DrawImage(imgWatermark, new Rectangle(xPosOfWm, yPosOfWm, markWidth, markHeight), 0, 0, wmWidth, wmHeight, GraphicsUnit.Pixel, imageAttributes); //最后的步骤将是使用新的Bitmap代替原来的Image。 销毁两个Graphic对象。然后把Image 保存到文件系统。 imgPhoto = bmWatermark; grPhoto.Dispose(); grWatermark.Dispose(); imgPhoto.Save(rDstImgPath, ImageFormat.Jpeg); imgPhoto.Dispose(); imgWatermark.Dispose(); }
作者博客地址:http://blog.csdn.net/chr23899
程序下载地址:http://download.csdn.net/detail/chr23899/8940175
- 单个图片、批量图片添加自定义水印内容小程序开发
- iOS开发-自己定义后台显示图片(iOS7-Background Fetch的应用)
- iOS - 自己定义alertView,继承自UIView,能够加入子视图,标题图片+文字
- 给phpcms v9加入一个主题radio无线电button,它可反复使用,以创建不同的专题部分内容编辑器,添加一个主题来定义自己的领域
- 二代旅游网站程序V1使用手册(九):图片水印配置及批量水印的功能
- 微信小程序开发关于分享功能中怎么定义同一个页面中不同的按钮触发不同的分享内容,或者右上角的分享和页面中分享
- Swift开发教程--怎样自己定义TabBarItem的图片显示
- 自己编写程序批量合并多个“规范定义电子名片(vCard)”文件
- Java图片水印生成器代码 - 批量给自己的网站图片加水印
- 跟我一起,利用bitcms内容管理系统从0到1学习小程序开发:三、上传图片到服务器
- C++开发人脸性别识别教程(10)——加入图片的人脸检測程序
- iOS开发-自己定义重用机制给ScrollerView加入子视图
- 二代旅游网站程序V1使用手册(九):图片水印配置及批量水印的功能
- 自己编写程序批量合并多个“规范定义电子名片(vCard)”文件
- 发布一款给图片批量加水印的程序PicNet V1.0
- Visual Studio 2017中使用正则修改部分内容 如何使用ILAsm与ILDasm修改.Net exe(dll)文件 C#学习-图解教程(1):格式化数字字符串 小程序开发之图片转Base64(C#、.Net) jquery遍历table为每一个单元格取值及赋值 。net加密解密相关方法 .net关于坐标之间一些简单操作
- PHP经典的给图片加水印程序
- PHP经典的给图片加水印程序
- PHP经典的给图片加水印程序
- ASP程序给上传的图片增添水印效果!