0

我们通过从原始位图创建一个新的 CroppedImage 在我们的 WPF 应用程序中显示裁剪的图像。但是当我们查看应用程序内存时,显示裁剪图像和显示原始图像是一样的。这可能并不奇怪,因为 CroppedImage 保留了对原始位图的引用。但是,是否有可能在不引用原始位图的情况下将裁剪的图像创建为新的位图图像,并通过这样做减少应用程序内存?

一些有关如何执行此操作的代码将不胜感激。

感谢帮助!

[编辑] 这是创建裁剪图像的代码:

 public class TheImage : ViewModelBase
{
    public BitmapSource CroppedImage { get; private set; }

    public TheImage(byte[] imageData)
    {
        var bitmapImage = CreateBitmapSource(imageData);
        var croppingRectangle = CalculateCropRectangle(bitmapImage.PixelWidth, bitmapImage.PixelHeight);

        CroppedImage = new CroppedBitmap(bitmapImage, croppingRectangle);

    }

    private static BitmapImage CreateBitmapSource(byte[] imageData)
    {
        var bitmapImage = new BitmapImage();
        bitmapImage.BeginInit();
        bitmapImage.StreamSource = new MemoryStream(imageData);
        bitmapImage.EndInit();
        return bitmapImage;
    }

    private static Int32Rect CalculateCropRectangle(int pixelWidth, int pixelHeight)
    {
        int width = 256;
        int height = 256;

        int x = (pixelWidth - width) / 2;
        int y = (pixelHeight - height) / 2;

        return new Int32Rect(x, y, width, height);
    }
}
4

0 回答 0