ImgHelper.cs 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. using System;
  2. using System.Drawing;
  3. using System.Drawing.Imaging;
  4. using System.IO;
  5. namespace LJLib.Tools.Utils
  6. {
  7. public static class ImgHelper
  8. {
  9. public static Bitmap NewBMPFrom(Image img, int width, int height)
  10. {
  11. var oImg = new Bitmap(width, height);
  12. using (var g = Graphics.FromImage(oImg))
  13. {
  14. var srcRect = new Rectangle(0, 0, img.Width, img.Height);
  15. var dstRect = new Rectangle();
  16. if (srcRect.Width != oImg.Width || srcRect.Height != oImg.Height)
  17. {
  18. if (srcRect.Width * oImg.Height > srcRect.Height * oImg.Width) // 按宽度拉伸
  19. {
  20. dstRect.Width = oImg.Width;
  21. dstRect.Height = dstRect.Width * srcRect.Height / srcRect.Width;
  22. dstRect.X = 0;
  23. dstRect.Y = (oImg.Height - dstRect.Height) / 2;
  24. }
  25. else // 按高度拉伸
  26. {
  27. dstRect.Height = oImg.Height;
  28. dstRect.Width = dstRect.Height * srcRect.Width / srcRect.Height;
  29. dstRect.X = (oImg.Width - dstRect.Width) / 2;
  30. dstRect.Y = 0;
  31. }
  32. }
  33. else // 不拉伸
  34. {
  35. dstRect.Width = oImg.Width;
  36. dstRect.Height = oImg.Height;
  37. dstRect.X = 0; //(oImg.Width - dstRect.Width) / 2;
  38. dstRect.Y = 0; //(oImg.Height - dstRect.Height) / 2;
  39. }
  40. var transparent = Color.FromArgb(0, 255, 255, 255);
  41. g.Clear(transparent);
  42. g.DrawImage(img, dstRect, srcRect, GraphicsUnit.Pixel);
  43. g.Save();
  44. oImg.MakeTransparent(transparent);
  45. }
  46. return oImg;
  47. }
  48. /// <summary>
  49. /// Byte数组 转 Image
  50. /// </summary>
  51. /// <param name="buffer"></param>
  52. /// <returns></returns>
  53. public static Image BytesToImage(byte[] buffer)
  54. {
  55. MemoryStream ms = new MemoryStream(buffer);
  56. Image image = System.Drawing.Image.FromStream(ms);
  57. return image;
  58. }
  59. /// <summary>
  60. /// Image 转 Byte数组
  61. /// </summary>
  62. /// <param name="image"></param>
  63. /// <returns></returns>
  64. public static byte[] ImageToBytes(Image image)
  65. {
  66. ImageFormat format = image.RawFormat;
  67. using (MemoryStream ms = new MemoryStream())
  68. {
  69. if (format.Equals(ImageFormat.Jpeg))
  70. {
  71. image.Save(ms, ImageFormat.Jpeg);
  72. }
  73. else if (format.Equals(ImageFormat.Png))
  74. {
  75. image.Save(ms, ImageFormat.Png);
  76. }
  77. else if (format.Equals(ImageFormat.Bmp))
  78. {
  79. image.Save(ms, ImageFormat.Bmp);
  80. }
  81. else if (format.Equals(ImageFormat.Gif))
  82. {
  83. image.Save(ms, ImageFormat.Gif);
  84. }
  85. else if (format.Equals(ImageFormat.Icon))
  86. {
  87. image.Save(ms, ImageFormat.Icon);
  88. }
  89. byte[] buffer = new byte[ms.Length];
  90. //Image.Save()会改变MemoryStream的Position,需要重新Seek到Begin
  91. ms.Seek(0, SeekOrigin.Begin);
  92. ms.Read(buffer, 0, buffer.Length);
  93. return buffer;
  94. }
  95. }
  96. /// <summary>
  97. /// 添加水印
  98. /// </summary>
  99. /// <param name="image">处理图片</param>
  100. /// <param name="addText">添加文字</param>
  101. /// <param name="color">文字颜色</param>
  102. /// <param name="font">文字字体</param>
  103. /// <param name="pos_x">文字位置--x</param>
  104. /// <param name="pos_y">文字位置--y</param>
  105. /// <returns></returns>
  106. public static Image AddWaterMark(Image image, string addText, Color color, Font font, string pos_x, string pos_y) //添加文本水印函数
  107. {
  108. try
  109. {
  110. Graphics g = Graphics.FromImage(image);
  111. g.DrawImage(image, 0, 0, image.Width, image.Height); //填充原图
  112. Brush b = new SolidBrush(color); //获取颜色
  113. g.DrawString(addText, font, b, Convert.ToInt16(pos_x), Convert.ToInt16(pos_y)); //在原图中加上水印文字
  114. g.Dispose();
  115. return image;
  116. }
  117. catch (Exception ex)
  118. {
  119. throw;
  120. }
  121. }
  122. /// <summary>
  123. /// Byte数组 图片添加水印 返回 Byte数组
  124. /// </summary>
  125. /// <param name="byteArray"></param>
  126. /// <param name="addText"></param>
  127. /// <param name="color"></param>
  128. /// <param name="font"></param>
  129. /// <param name="pos_x"></param>
  130. /// <param name="pos_y"></param>
  131. /// <returns></returns>
  132. public static Byte[] AddWaterMarkWithByte(Byte[] byteArray, string addText, Color color, Font font, string pos_x, string pos_y) //添加文本水印函数
  133. {
  134. try
  135. {
  136. var image = ImgHelper.BytesToImage(byteArray);
  137. Image newimage = ImgHelper.AddWaterMark(image, addText, color, font, pos_x, pos_y);
  138. return ImageToBytes(newimage);
  139. }
  140. catch (Exception ex)
  141. {
  142. throw;
  143. }
  144. }
  145. }
  146. }