How to crop image and save into ImageSource in WPF?

后端 未结 2 1812
滥情空心
滥情空心 2021-02-08 23:56

I am new learner to WPF. here I got a question. I have a image, width:360, height:360. Here I want to crop this image like below:

( 0,0 ) to (120,120) save to the fi

2条回答
  •  猫巷女王i
    2021-02-09 00:23

    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));
    }
    

提交回复
热议问题