平安校园
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。
 
 
 
 
 
 

224 行
5.8 KiB

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