본문 바로가기

Dev::DotNet/WPF

BitmapSource 이미지 일부 잘라내기

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
public BitmapSource CropImage(BitmapSource aImage, Rect aCropRect)
{
    if (aImage == null)
        throw new ArgumentNullException("Image");
 
    var rect = new Rect(00, aImage.PixelWidth, aImage.PixelHeight);
            
    if (!rect.IntersectsWith(aCropRect))
        throw new ArgumentOutOfRangeException("Crop Rect");
 
    rect.Intersect(aCropRect);
 
    return 
        new CroppedBitmap(
                        aImage, 
                        new Int32Rect(
                            (int)rect.X,
                            (int)rect.Y,
                            (int)rect.Width,
                            (int)rect.Height));
}
cs