3
我尝试在内存中压缩ImageSource
和MagickImage
。但它消耗太多内存。使用VS性能工具,每次调用此方法都会消耗大量内存。它一旦存在就应该消耗0Mb,对吧?压缩映像时C#中的内存泄漏
internal static System.Windows.Media.ImageSource compressImage(System.Windows.Media.ImageSource ims)
{
using (MemoryStream stream = new MemoryStream())
{
using (MemoryStream outS = new MemoryStream())
{
BitmapSource bs = ims as BitmapSource;
JpegBitmapEncoder encoder = new JpegBitmapEncoder();
BitmapFrame bf = BitmapFrame.Create(bs);
//encoder.Frames.Add(BitmapFrame.Create(image1.Source));
encoder.Frames.Add(bf);
encoder.Save(stream);
stream.Flush();
try
{
// Read first frame of gif image
using (MagickImage image = new MagickImage(stream))
{
image.Quality = 75;
image.Resize(new Percentage(0.65));
image.Density = new Density(200, DensityUnit.PixelsPerInch);
image.Write(outS);
}
stream.Close();
BitmapImage bitmap = new BitmapImage();
bitmap.BeginInit();
bitmap.CacheOption = BitmapCacheOption.OnLoad;
outS.Position = 0;
bitmap.StreamSource = outS;
//
bitmap.EndInit();
//bitmap.Freeze();
outS.Flush();
outS.Close();
ims = null;
return bitmap;
}
catch (Exception e)
{
return null;
}
}
}
}
}
当你使用'return image.ToBitmapSource()'时,会发生什么? – dlemstra
@dlemstra没有这样的方法 – Xiaokun
您使用的是最新版本的Magick.NET吗?图像应该有这种方法。也许你正在使用.NET Core版本? – dlemstra