ImageUtil.cs 1.0 KB

123456789101112131415161718192021222324252627282930
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.Windows;
  7. using System.Windows.Media;
  8. using System.Windows.Media.Imaging;
  9. namespace FWindSoft.Wpf
  10. {
  11. public static class ImageUtil
  12. {
  13. /// <summary>
  14. /// 获取图像的中间区域
  15. /// </summary>
  16. /// <param name="source"></param>
  17. /// <returns></returns>
  18. public static ImageSource GetCenterCropped(BitmapSource source)
  19. {
  20. BitmapSource imageSource = source;
  21. double originWidth = imageSource.Width / 96 * imageSource.DpiX;
  22. double originHeight = imageSource.Height / 96 * imageSource.DpiY;
  23. double useSize = Math.Min(originWidth, originHeight);
  24. var rect = new Int32Rect((int)((originWidth - useSize) / 2), (int)((originHeight - useSize) / 2), (int)useSize - 1, (int)useSize - 1);
  25. var cropped = new CroppedBitmap(imageSource, rect);
  26. return cropped;
  27. }
  28. }
  29. }