减少C#WPF中裁剪图像的应用程序内存
我们通过从原始位图创建新的裁剪图像,在WPF应用程序中显示裁剪后的图像。但当我们看应用程序内存时,显示裁剪图像和显示原始图像是一样的。这可能并不奇怪,因为CroppedImage保留了对原始位图的引用。但是,是否有可能在不引用原始位图的情况下,通过减少应用程序内存的方式,将裁剪后的图像创建为新位图图像 一些代码如何做到这一点将是高度赞赏 谢谢你的帮助 [编辑] 以下是创建裁剪图像的代码:减少C#WPF中裁剪图像的应用程序内存,c#,wpf,bitmapimage,bitmapsource,C#,Wpf,Bitmapimage,Bitmapsource,我们通过从原始位图创建新的裁剪图像,在WPF应用程序中显示裁剪后的图像。但当我们看应用程序内存时,显示裁剪图像和显示原始图像是一样的。这可能并不奇怪,因为CroppedImage保留了对原始位图的引用。但是,是否有可能在不引用原始位图的情况下,通过减少应用程序内存的方式,将裁剪后的图像创建为新位图图像 一些代码如何做到这一点将是高度赞赏 谢谢你的帮助 [编辑] 以下是创建裁剪图像的代码: public class TheImage : ViewModelBase { public Bi
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);
}
}
这取决于如何创建裁剪图像。请在这里提供一些代码。好的,我编辑了问题并添加了一些代码。