C#给图片加水印,可设置透明度

 

[c-sharp] view plain copy print ?
  1. /// <summary>
  2. /// Creating a Watermarked Photograph with GDI+ for .NET
  3. /// </summary>
  4. /// <param name="rSrcImgPath">原始图片的物理路径</param>
  5. /// <param name="rMarkImgPath">水印图片的物理路径</param>
  6. /// <param name="rMarkText">水印文字(不显示水印文字设为空串)</param>
  7. /// <param name="rDstImgPath">输出合成后的图片的物理路径</param>
  8. public void BuildWatermark(string rSrcImgPath, string rMarkImgPath, string rMarkText, string rDstImgPath)
  9. {
  10. //以下(代码)从一个指定文件创建了一个Image 对象,然后为它的 Width 和 Height定义变量。
  11. //这些长度待会被用来建立一个以24 bits 每像素的格式作为颜色数据的Bitmap对象。
  12. Image imgPhoto = Image.FromFile(rSrcImgPath);
  13. int phWidth = imgPhoto.Width;
  14. int phHeight = imgPhoto.Height;
  15. Bitmap bmPhoto = new Bitmap(phWidth, phHeight, PixelFormat.Format24bppRgb);
  16. bmPhoto.SetResolution(72, 72);
  17. Graphics grPhoto = Graphics.FromImage(bmPhoto);
  18. //这个代码载入水印图片,水印图片已经被保存为一个BMP文件,以绿色(A=0,R=0,G=255,B=0)作为背景颜色。
  19. //再一次,会为它的Width 和Height定义一个变量。
  20. Image imgWatermark = new Bitmap(rMarkImgPath);
  21. int wmWidth = imgWatermark.Width;
  22. int wmHeight = imgWatermark.Height;
  23. //这个代码以100%它的原始大小绘制imgPhoto 到Graphics 对象的(x=0,y=0)位置。
  24. //以后所有的绘图都将发生在原来照片的顶部。
  25. grPhoto.SmoothingMode = SmoothingMode.AntiAlias;
  26. grPhoto.DrawImage(
  27. imgPhoto,
  28. new Rectangle(0, 0, phWidth, phHeight),
  29. 0,
  30. 0,
  31. phWidth,
  32. phHeight,
  33. GraphicsUnit.Pixel);
  34. //为了最大化版权信息的大小,我们将测试7种不同的字体大小来决定我们能为我们的照片宽度使用的可能的最大大小。
  35. //为了有效地完成这个,我们将定义一个整型数组,接着遍历这些整型值测量不同大小的版权字符串。
  36. //一旦我们决定了可能的最大大小,我们就退出循环,绘制文本
  37. int[] sizes = new int[] { 16, 14, 12, 10, 8, 6, 4 };
  38. Font crFont = null;
  39. SizeF crSize = new SizeF();
  40. for (int i = 0;i < 7;i++)
  41. {
  42. crFont = new Font("arial", sizes[i],
  43. FontStyle.Bold);
  44. crSize = grPhoto.MeasureString(rMarkText,
  45. crFont);
  46. if ((ushort)crSize.Width < (ushort)phWidth)
  47. break;
  48. }
  49. //因为所有的照片都有各种各样的高度,所以就决定了从图象底部开始的5%的位置开始。
  50. //使用rMarkText字符串的高度来决定绘制字符串合适的Y坐标轴。
  51. //通过计算图像的中心来决定X轴,然后定义一个StringFormat 对象,设置StringAlignment 为Center。
  52. int yPixlesFromBottom = (int)(phHeight * .05);
  53. float yPosFromBottom = ((phHeight -
  54. yPixlesFromBottom) - (crSize.Height / 2));
  55. float xCenterOfImg = (phWidth / 2);
  56. StringFormat StrFormat = new StringFormat();
  57. StrFormat.Alignment = StringAlignment.Center;
  58. //现在我们已经有了所有所需的位置坐标来使用60%黑色的一个Color(alpha值153)创建一个SolidBrush 。
  59. //在偏离右边1像素,底部1像素的合适位置绘制版权字符串。
  60. //这段偏离将用来创建阴影效果。使用Brush重复这样一个过程,在前一个绘制的文本顶部绘制同样的文本。
  61. SolidBrush semiTransBrush2 =
  62. new SolidBrush(Color.FromArgb(153, 0, 0, 0));
  63. grPhoto.DrawString(rMarkText,
  64. crFont,
  65. semiTransBrush2,
  66. new PointF(xCenterOfImg + 1, yPosFromBottom + 1),
  67. StrFormat);
  68. SolidBrush semiTransBrush = new SolidBrush(
  69. Color.FromArgb(153, 255, 255, 255));
  70. grPhoto.DrawString(rMarkText,
  71. crFont,
  72. semiTransBrush,
  73. new PointF(xCenterOfImg, yPosFromBottom),
  74. StrFormat);
  75. //根据前面修改后的照片创建一个Bitmap。把这个Bitmap载入到一个新的Graphic对象。
  76. Bitmap bmWatermark = new Bitmap(bmPhoto);
  77. bmWatermark.SetResolution(
  78. imgPhoto.HorizontalResolution,
  79. imgPhoto.VerticalResolution);
  80. Graphics grWatermark =
  81. Graphics.FromImage(bmWatermark);
  82. //通过定义一个ImageAttributes 对象并设置它的两个属性,我们就是实现了两个颜色的处理,以达到半透明的水印效果。
  83. //处理水印图象的第一步是把背景图案变为透明的(Alpha=0, R=0, G=0, B=0)。我们使用一个Colormap 和定义一个RemapTable来做这个。
  84. //就像前面展示的,我的水印被定义为100%绿色背景,我们将搜到这个颜色,然后取代为透明。
  85. ImageAttributes imageAttributes =
  86. new ImageAttributes();
  87. ColorMap colorMap = new ColorMap();
  88. colorMap.OldColor = Color.FromArgb(255, 0, 255, 0);
  89. colorMap.NewColor = Color.FromArgb(0, 0, 0, 0);
  90. ColorMap[] remapTable = { colorMap };
  91. //第二个颜色处理用来改变水印的不透明性。
  92. //通过应用包含提供了坐标的RGBA空间的5x5矩阵来做这个。
  93. //通过设定第三行、第三列为0.3f我们就达到了一个不透明的水平。结果是水印会轻微地显示在图象底下一些。
  94. imageAttributes.SetRemapTable(remapTable,
  95. ColorAdjustType.Bitmap);
  96. float[][] colorMatrixElements = {
  97. new float[] {1.0f, 0.0f, 0.0f, 0.0f, 0.0f},
  98. new float[] {0.0f, 1.0f, 0.0f, 0.0f, 0.0f},
  99. new float[] {0.0f, 0.0f, 1.0f, 0.0f, 0.0f},
  100. new float[] {0.0f, 0.0f, 0.0f, 0.3f, 0.0f},
  101. new float[] {0.0f, 0.0f, 0.0f, 0.0f, 1.0f}
  102. };
  103. ColorMatrix wmColorMatrix = new
  104. ColorMatrix(colorMatrixElements);
  105. imageAttributes.SetColorMatrix(wmColorMatrix,
  106. ColorMatrixFlag.Default,
  107. ColorAdjustType.Bitmap);
  108. //随着两个颜色处理加入到imageAttributes 对象,我们现在就能在照片右手边上绘制水印了。
  109. //我们会偏离10像素到底部,10像素到左边。
  110. int markWidth;
  111. int markHeight;
  112. //mark比原来的图宽
  113. if (phWidth <= wmWidth)
  114. {
  115. markWidth = phWidth - 10;
  116. markHeight = (markWidth * wmHeight) / wmWidth;
  117. }
  118. else if (phHeight <= wmHeight)
  119. {
  120. markHeight = phHeight - 10;
  121. markWidth = (markHeight * wmWidth) / wmHeight;
  122. }
  123. else
  124. {
  125. markWidth = wmWidth;
  126. markHeight = wmHeight;
  127. }
  128. int xPosOfWm = ((phWidth - markWidth) - 10);
  129. int yPosOfWm = 10;
  130. grWatermark.DrawImage(imgWatermark,
  131. new Rectangle(xPosOfWm, yPosOfWm, markWidth,
  132. markHeight),
  133. 0,
  134. 0,
  135. wmWidth,
  136. wmHeight,
  137. GraphicsUnit.Pixel,
  138. imageAttributes);
  139. //最后的步骤将是使用新的Bitmap取代原来的Image。 销毁两个Graphic对象,然后把Image 保存到文件系统。
  140. imgPhoto = bmWatermark;
  141. grPhoto.Dispose();
  142. grWatermark.Dispose();
  143. imgPhoto.Save(rDstImgPath, ImageFormat.Jpeg);
  144. imgPhoto.Dispose();
  145. imgWatermark.Dispose();
  146. }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值