平安校园
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ImageUtil.cs 5.8 KiB

4 months ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. 
  2. //
  3. namespace SafeCampus.Core.Utils;
  4. public static class ImageUtil
  5. {
  6. /// <summary>
  7. /// 图片转换成base64
  8. /// </summary>
  9. /// <param name="img"></param>
  10. /// <returns></returns>
  11. public static string ImgToBase64String(SKImage img)
  12. {
  13. try
  14. {
  15. var p = img.Encode(SKEncodedImageFormat.Png, 100);
  16. var arr = p.ToArray();
  17. return Convert.ToBase64String(arr);
  18. }
  19. catch
  20. {
  21. return string.Empty;
  22. }
  23. }
  24. /// <summary>
  25. /// 图片转换成base64
  26. /// </summary>
  27. /// <param name="bmp"></param>
  28. /// <returns></returns>
  29. public static string ImgToBase64String(this SKBitmap bmp)
  30. {
  31. try
  32. {
  33. var img = SKImage.FromBitmap(bmp);
  34. var p = img.Encode(SKEncodedImageFormat.Png, 100);
  35. var arr = p.ToArray();
  36. return Convert.ToBase64String(arr);
  37. }
  38. catch
  39. {
  40. return string.Empty;
  41. }
  42. }
  43. /// <summary>
  44. /// base64转bitmap
  45. /// </summary>
  46. /// <param name="base64String"></param>
  47. /// <returns></returns>
  48. public static Bitmap GetBitmapFromBase64(this string base64String)
  49. {
  50. var b = Convert.FromBase64String(base64String);
  51. var ms = new MemoryStream(b);
  52. var bitmap = new Bitmap(ms);
  53. ms.Close();
  54. return bitmap;
  55. }
  56. /// <summary>
  57. /// base64转bitmap
  58. /// </summary>
  59. /// <param name="base64String"></param>
  60. /// <returns></returns>
  61. public static SKBitmap GetSkBitmapFromBase64(this string base64String)
  62. {
  63. var b = Convert.FromBase64String(base64String);
  64. var bitmap = SKBitmap.Decode(b);
  65. return bitmap;
  66. }
  67. /// <summary>
  68. /// base64转image格式
  69. /// </summary>
  70. /// <param name="base64String"></param>
  71. /// <returns></returns>
  72. public static string ToImageBase64(this string base64String)
  73. {
  74. return "data:image/png;base64," + base64String;
  75. }
  76. /// <summary>
  77. /// 重新修改尺寸
  78. /// </summary>
  79. /// <param name="imgToResize">图片</param>
  80. /// <param name="size">尺寸</param>
  81. /// <returns></returns>
  82. public static Bitmap ResizeImage(Image imgToResize, Size size)
  83. {
  84. //获取图片宽度
  85. var sourceWidth = imgToResize.Width;
  86. //获取图片高度
  87. var sourceHeight = imgToResize.Height;
  88. float nPercent;
  89. //计算宽度的缩放比例
  90. var nPercentW = size.Width / (float)sourceWidth;
  91. //计算高度的缩放比例
  92. var nPercentH = size.Height / (float)sourceHeight;
  93. if (nPercentH < nPercentW)
  94. nPercent = nPercentH;
  95. else
  96. nPercent = nPercentW;
  97. //期望的宽度
  98. var destWidth = (int)(sourceWidth * nPercent);
  99. //期望的高度
  100. var destHeight = (int)(sourceHeight * nPercent);
  101. var b = new Bitmap(destWidth, destHeight);
  102. var g = Graphics.FromImage(b);
  103. g.InterpolationMode = InterpolationMode.HighQualityBicubic;
  104. //绘制图像
  105. g.DrawImage(imgToResize, 0, 0, destWidth,
  106. destHeight);
  107. g.Dispose();
  108. return b;
  109. }
  110. /// <summary>
  111. /// Resize图片
  112. /// </summary>
  113. /// <param name="bmp">原始Bitmap </param>
  114. /// <param name="newW">新的宽度</param>
  115. /// <param name="newH">新的高度</param>
  116. /// <returns>处理以后的图片</returns>
  117. public static Bitmap ResizeImage(this Bitmap bmp, int newW, int newH)
  118. {
  119. try
  120. {
  121. var b = new Bitmap(newW, newH);
  122. var g = Graphics.FromImage(b);
  123. // 插值算法的质量
  124. g.InterpolationMode = InterpolationMode.HighQualityBicubic;
  125. g.DrawImage(bmp, new Rectangle(0, 0, newW, newH), new Rectangle(0, 0, bmp.Width, bmp.Height), GraphicsUnit.Pixel);
  126. g.Dispose();
  127. return b;
  128. }
  129. catch
  130. {
  131. return null;
  132. }
  133. }
  134. /// <summary>
  135. /// Resize图片
  136. /// </summary>
  137. /// <param name="bmp">原始Bitmap </param>
  138. /// <param name="newW">新的宽度</param>
  139. /// <param name="newH">新的高度</param>
  140. /// <returns>处理以后的图片</returns>
  141. public static SKBitmap ResizeImage(this SKBitmap bmp, int newW, int newH)
  142. {
  143. try
  144. {
  145. var b = new SKBitmap(newW, newH);
  146. var resized = b.Resize(new SKImageInfo(newW, newH), SKFilterQuality.High);
  147. if (resized is null)
  148. {
  149. return null;
  150. }
  151. var image = SKImage.FromBitmap(resized);
  152. return b;
  153. }
  154. catch
  155. {
  156. return null;
  157. }
  158. }
  159. /// <summary>
  160. /// 获取缩略图
  161. /// </summary>
  162. /// <param name="bmp"></param>
  163. /// <param name="w">宽</param>
  164. /// <param name="h">高</param>
  165. /// <returns></returns>
  166. public static Image GetPicThumbnail(this Bitmap bmp, int w, int h)
  167. {
  168. try
  169. {
  170. var thumbnail = bmp.GetThumbnailImage(w, h, () => false, IntPtr.Zero);
  171. return thumbnail;
  172. }
  173. catch (Exception ex)
  174. {
  175. return null;
  176. }
  177. }
  178. /// <summary>
  179. /// 获取缩略图
  180. /// </summary>
  181. /// <param name="bmp"></param>
  182. /// <param name="w">宽</param>
  183. /// <param name="h">高</param>
  184. /// <returns></returns>
  185. public static SKImage GetPicThumbnail(this SKBitmap bmp, int w, int h)
  186. {
  187. try
  188. {
  189. var resized = bmp.Resize(new SKImageInfo(w, h), SKFilterQuality.Medium);
  190. if (resized is null)
  191. {
  192. return null;
  193. }
  194. var image = SKImage.FromBitmap(resized);
  195. return image;
  196. }
  197. catch (Exception)
  198. {
  199. return null;
  200. }
  201. }
  202. }