市长热线演示版

WaterMark.cs 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. using System;
  2. using System.Web;
  3. using System.Drawing;
  4. using System.Drawing.Imaging;
  5. using System.Drawing.Drawing2D;
  6. using System.IO;
  7. namespace HySoft.Common
  8. {
  9. public class WaterMark
  10. {
  11. /// <summary>
  12. /// 图片水印
  13. /// </summary>
  14. /// <param name="imgPath">服务器图片相对路径</param>
  15. /// <param name="filename">保存文件名</param>
  16. /// <param name="watermarkFilename">水印文件相对路径</param>
  17. /// <param name="watermarkStatus">图片水印位置 0=不使用 1=左上 2=中上 3=右上 4=左中 9=右下</param>
  18. /// <param name="quality">附加水印图片质量,0-100</param>
  19. /// <param name="watermarkTransparency">水印的透明度 1--10 10为不透明</param>
  20. public static void AddImageSignPic(string imgPath, string filename, string watermarkFilename, int watermarkStatus, int quality, int watermarkTransparency)
  21. {
  22. if(!File.Exists(Utils.GetMapPath(imgPath)))
  23. return;
  24. byte[] _ImageBytes = File.ReadAllBytes(Utils.GetMapPath(imgPath));
  25. Image img = Image.FromStream(new System.IO.MemoryStream(_ImageBytes));
  26. filename = Utils.GetMapPath(filename);
  27. if (watermarkFilename.StartsWith("/") == false)
  28. watermarkFilename = "/" + watermarkFilename;
  29. watermarkFilename = Utils.GetMapPath(watermarkFilename);
  30. if (!File.Exists(watermarkFilename))
  31. return;
  32. Graphics g = Graphics.FromImage(img);
  33. //设置高质量插值法
  34. //g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
  35. //设置高质量,低速度呈现平滑程度
  36. //g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
  37. Image watermark = new Bitmap(watermarkFilename);
  38. if (watermark.Height >= img.Height || watermark.Width >= img.Width)
  39. return;
  40. ImageAttributes imageAttributes = new ImageAttributes();
  41. ColorMap colorMap = new ColorMap();
  42. colorMap.OldColor = Color.FromArgb(255, 0, 255, 0);
  43. colorMap.NewColor = Color.FromArgb(0, 0, 0, 0);
  44. ColorMap[] remapTable = { colorMap };
  45. imageAttributes.SetRemapTable(remapTable, ColorAdjustType.Bitmap);
  46. float transparency = 0.5F;
  47. if (watermarkTransparency >= 1 && watermarkTransparency <= 10)
  48. transparency = (watermarkTransparency / 10.0F);
  49. float[][] colorMatrixElements = {
  50. new float[] {1.0f, 0.0f, 0.0f, 0.0f, 0.0f},
  51. new float[] {0.0f, 1.0f, 0.0f, 0.0f, 0.0f},
  52. new float[] {0.0f, 0.0f, 1.0f, 0.0f, 0.0f},
  53. new float[] {0.0f, 0.0f, 0.0f, transparency, 0.0f},
  54. new float[] {0.0f, 0.0f, 0.0f, 0.0f, 1.0f}
  55. };
  56. ColorMatrix colorMatrix = new ColorMatrix(colorMatrixElements);
  57. imageAttributes.SetColorMatrix(colorMatrix, ColorMatrixFlag.Default, ColorAdjustType.Bitmap);
  58. int xpos = 0;
  59. int ypos = 0;
  60. switch (watermarkStatus)
  61. {
  62. case 1:
  63. xpos = (int)(img.Width * (float).01);
  64. ypos = (int)(img.Height * (float).01);
  65. break;
  66. case 2:
  67. xpos = (int)((img.Width * (float).50) - (watermark.Width / 2));
  68. ypos = (int)(img.Height * (float).01);
  69. break;
  70. case 3:
  71. xpos = (int)((img.Width * (float).99) - (watermark.Width));
  72. ypos = (int)(img.Height * (float).01);
  73. break;
  74. case 4:
  75. xpos = (int)(img.Width * (float).01);
  76. ypos = (int)((img.Height * (float).50) - (watermark.Height / 2));
  77. break;
  78. case 5:
  79. xpos = (int)((img.Width * (float).50) - (watermark.Width / 2));
  80. ypos = (int)((img.Height * (float).50) - (watermark.Height / 2));
  81. break;
  82. case 6:
  83. xpos = (int)((img.Width * (float).99) - (watermark.Width));
  84. ypos = (int)((img.Height * (float).50) - (watermark.Height / 2));
  85. break;
  86. case 7:
  87. xpos = (int)(img.Width * (float).01);
  88. ypos = (int)((img.Height * (float).99) - watermark.Height);
  89. break;
  90. case 8:
  91. xpos = (int)((img.Width * (float).50) - (watermark.Width / 2));
  92. ypos = (int)((img.Height * (float).99) - watermark.Height);
  93. break;
  94. case 9:
  95. xpos = (int)((img.Width * (float).99) - (watermark.Width));
  96. ypos = (int)((img.Height * (float).99) - watermark.Height);
  97. break;
  98. }
  99. g.DrawImage(watermark, new Rectangle(xpos, ypos, watermark.Width, watermark.Height), 0, 0, watermark.Width, watermark.Height, GraphicsUnit.Pixel, imageAttributes);
  100. ImageCodecInfo[] codecs = ImageCodecInfo.GetImageEncoders();
  101. ImageCodecInfo ici = null;
  102. foreach (ImageCodecInfo codec in codecs)
  103. {
  104. if (codec.MimeType.IndexOf("jpeg") > -1)
  105. ici = codec;
  106. }
  107. EncoderParameters encoderParams = new EncoderParameters();
  108. long[] qualityParam = new long[1];
  109. if (quality < 0 || quality > 100)
  110. quality = 80;
  111. qualityParam[0] = quality;
  112. EncoderParameter encoderParam = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, qualityParam);
  113. encoderParams.Param[0] = encoderParam;
  114. if (ici != null)
  115. img.Save(filename, ici, encoderParams);
  116. else
  117. img.Save(filename);
  118. g.Dispose();
  119. img.Dispose();
  120. watermark.Dispose();
  121. imageAttributes.Dispose();
  122. }
  123. /// <summary>
  124. /// 文字水印
  125. /// </summary>
  126. /// <param name="imgPath">服务器图片相对路径</param>
  127. /// <param name="filename">保存文件名</param>
  128. /// <param name="watermarkText">水印文字</param>
  129. /// <param name="watermarkStatus">图片水印位置 0=不使用 1=左上 2=中上 3=右上 4=左中 9=右下</param>
  130. /// <param name="quality">附加水印图片质量,0-100</param>
  131. /// <param name="fontname">字体</param>
  132. /// <param name="fontsize">字体大小</param>
  133. public static void AddImageSignText(string imgPath, string filename, string watermarkText, int watermarkStatus, int quality, string fontname, int fontsize)
  134. {
  135. byte[] _ImageBytes = File.ReadAllBytes(Utils.GetMapPath(imgPath));
  136. Image img = Image.FromStream(new System.IO.MemoryStream(_ImageBytes));
  137. filename = Utils.GetMapPath(filename);
  138. Graphics g = Graphics.FromImage(img);
  139. Font drawFont = new Font(fontname, fontsize, FontStyle.Regular, GraphicsUnit.Pixel);
  140. SizeF crSize;
  141. crSize = g.MeasureString(watermarkText, drawFont);
  142. float xpos = 0;
  143. float ypos = 0;
  144. switch (watermarkStatus)
  145. {
  146. case 1:
  147. xpos = (float)img.Width * (float).01;
  148. ypos = (float)img.Height * (float).01;
  149. break;
  150. case 2:
  151. xpos = ((float)img.Width * (float).50) - (crSize.Width / 2);
  152. ypos = (float)img.Height * (float).01;
  153. break;
  154. case 3:
  155. xpos = ((float)img.Width * (float).99) - crSize.Width;
  156. ypos = (float)img.Height * (float).01;
  157. break;
  158. case 4:
  159. xpos = (float)img.Width * (float).01;
  160. ypos = ((float)img.Height * (float).50) - (crSize.Height / 2);
  161. break;
  162. case 5:
  163. xpos = ((float)img.Width * (float).50) - (crSize.Width / 2);
  164. ypos = ((float)img.Height * (float).50) - (crSize.Height / 2);
  165. break;
  166. case 6:
  167. xpos = ((float)img.Width * (float).99) - crSize.Width;
  168. ypos = ((float)img.Height * (float).50) - (crSize.Height / 2);
  169. break;
  170. case 7:
  171. xpos = (float)img.Width * (float).01;
  172. ypos = ((float)img.Height * (float).99) - crSize.Height;
  173. break;
  174. case 8:
  175. xpos = ((float)img.Width * (float).50) - (crSize.Width / 2);
  176. ypos = ((float)img.Height * (float).99) - crSize.Height;
  177. break;
  178. case 9:
  179. xpos = ((float)img.Width * (float).99) - crSize.Width;
  180. ypos = ((float)img.Height * (float).99) - crSize.Height;
  181. break;
  182. }
  183. g.DrawString(watermarkText, drawFont, new SolidBrush(Color.White), xpos + 1, ypos + 1);
  184. g.DrawString(watermarkText, drawFont, new SolidBrush(Color.Black), xpos, ypos);
  185. ImageCodecInfo[] codecs = ImageCodecInfo.GetImageEncoders();
  186. ImageCodecInfo ici = null;
  187. foreach (ImageCodecInfo codec in codecs)
  188. {
  189. if (codec.MimeType.IndexOf("jpeg") > -1)
  190. ici = codec;
  191. }
  192. EncoderParameters encoderParams = new EncoderParameters();
  193. long[] qualityParam = new long[1];
  194. if (quality < 0 || quality > 100)
  195. quality = 80;
  196. qualityParam[0] = quality;
  197. EncoderParameter encoderParam = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, qualityParam);
  198. encoderParams.Param[0] = encoderParam;
  199. if (ici != null)
  200. img.Save(filename, ici, encoderParams);
  201. else
  202. img.Save(filename);
  203. g.Dispose();
  204. img.Dispose();
  205. }
  206. }
  207. }