且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在WPF中裁剪图像并保存到ImageSource?

更新时间:2023-12-05 16:41:28

Use CroppedBitmap to do this:

private void CutImage(string img)
{
    int count = 0;

    BitmapImage src = new BitmapImage();
    src.BeginInit();
    src.UriSource = new Uri(img, UriKind.Relative);
    src.CacheOption = BitmapCacheOption.OnLoad;
    src.EndInit();

    for (int i = 0; i < 3; i++)
        for (int j = 0; j < 3; j++)
            objImg[count++] = new CroppedBitmap(src, new Int32Rect(j * 120, i * 120, 120, 120));
}