123456789101112131415161718192021222324252627282930 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows;
- using System.Windows.Media;
- using System.Windows.Media.Imaging;
- namespace FWindSoft.Wpf
- {
- public static class ImageUtil
- {
- /// <summary>
- /// 获取图像的中间区域
- /// </summary>
- /// <param name="source"></param>
- /// <returns></returns>
- public static ImageSource GetCenterCropped(BitmapSource source)
- {
- BitmapSource imageSource = source;
- double originWidth = imageSource.Width / 96 * imageSource.DpiX;
- double originHeight = imageSource.Height / 96 * imageSource.DpiY;
- double useSize = Math.Min(originWidth, originHeight);
- var rect = new Int32Rect((int)((originWidth - useSize) / 2), (int)((originHeight - useSize) / 2), (int)useSize - 1, (int)useSize - 1);
- var cropped = new CroppedBitmap(imageSource, rect);
- return cropped;
- }
- }
- }
|