ImageWatermarker.cs 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. using System;
  2. using System.Drawing;
  3. using System.Drawing.Text;
  4. using System.IO;
  5. namespace Masuit.Tools.Media
  6. {
  7. public class ImageWatermarker
  8. {
  9. /// <summary>
  10. /// 是否跳过小缩略图
  11. /// </summary>
  12. public bool SkipWatermarkForSmallImages { get; set; }
  13. /// <summary>
  14. /// 小图像素大小
  15. /// </summary>
  16. public int SmallImagePixelsThreshold { get; set; }
  17. private readonly Stream _stream;
  18. public ImageWatermarker(Stream originStream)
  19. {
  20. _stream = originStream;
  21. }
  22. /// <summary>
  23. /// 添加水印
  24. /// </summary>
  25. /// <param name="watermarkText">水印文字</param>
  26. /// <param name="color">水印颜色</param>
  27. /// <param name="watermarkPosition">水印位置</param>
  28. /// <param name="textPadding">边距</param>
  29. /// <param name="fontSize">字体大小</param>
  30. /// <param name="font">字体</param>
  31. /// <param name="textAntiAlias">不提示的情况下使用抗锯齿标志符号位图来绘制每个字符。
  32. /// 由于抗锯齿质量就越好。
  33. /// 因为关闭了提示,词干宽度之间的差异可能非常明显。</param>
  34. /// <returns></returns>
  35. public MemoryStream AddWatermark(string watermarkText, Color color, WatermarkPosition watermarkPosition = WatermarkPosition.BottomRight, int textPadding = 10, int fontSize = 20, Font font = null, bool textAntiAlias = true)
  36. {
  37. using var img = Image.FromStream(_stream);
  38. if (SkipWatermarkForSmallImages && (img.Height < Math.Sqrt(SmallImagePixelsThreshold) || img.Width < Math.Sqrt(SmallImagePixelsThreshold)))
  39. {
  40. return _stream.SaveAsMemoryStream();
  41. }
  42. using var graphic = Graphics.FromImage(img);
  43. if (textAntiAlias)
  44. {
  45. graphic.TextRenderingHint = TextRenderingHint.AntiAlias;
  46. }
  47. using var brush = new SolidBrush(color);
  48. if (img.Width / fontSize > 50)
  49. {
  50. fontSize = img.Width / 50;
  51. }
  52. using var f = font ?? new Font(FontFamily.GenericSansSerif, fontSize, FontStyle.Bold, GraphicsUnit.Pixel);
  53. var textSize = graphic.MeasureString(watermarkText, f);
  54. int x, y;
  55. textPadding += (img.Width - 1000) / 100;
  56. switch (watermarkPosition)
  57. {
  58. case WatermarkPosition.TopLeft:
  59. x = textPadding;
  60. y = textPadding;
  61. break;
  62. case WatermarkPosition.TopRight:
  63. x = img.Width - (int)textSize.Width - textPadding;
  64. y = textPadding;
  65. break;
  66. case WatermarkPosition.BottomLeft:
  67. x = textPadding;
  68. y = img.Height - (int)textSize.Height - textPadding;
  69. break;
  70. case WatermarkPosition.BottomRight:
  71. x = img.Width - (int)textSize.Width - textPadding;
  72. y = img.Height - (int)textSize.Height - textPadding;
  73. break;
  74. default:
  75. x = textPadding;
  76. y = textPadding;
  77. break;
  78. }
  79. graphic.DrawString(watermarkText, f, brush, new Point(x, y));
  80. var ms = new MemoryStream();
  81. img.Save(ms, img.RawFormat);
  82. ms.Position = 0;
  83. return ms;
  84. }
  85. }
  86. }