我有一个 WPF 应用程序,它有一个图像列表框。现在我正在使用 BitmapImage
和 BitmapCacheOption.OnLoad
加载图像。
问题是当有很多图像时,由于图像的大小,RAM 使用率飙升。
如何创建要在列表框中显示的原件的缩略图?
它可能必须被缓存,因为目录中的图像文件可能在应用程序运行时被删除或修改。
最佳答案
The problem is that when there are a lot of images, the RAM usage sky rockets due to the size of the images.
C# 示例来自:http://msdn.microsoft.com/en-us/library/ms748873.aspx
// Create source
BitmapImage myBitmapImage = new BitmapImage();
// BitmapImage.UriSource must be in a BeginInit/EndInit block
myBitmapImage.BeginInit();
myBitmapImage.UriSource = new Uri(@"C:\Water Lilies.jpg");
// To save significant application memory, set the DecodePixelWidth or
// DecodePixelHeight of the BitmapImage value of the image source to the desired
// height or width of the rendered image. If you don't do this, the application will
// cache the image as though it were rendered as its normal size rather then just
// the size that is displayed.
// Note: In order to preserve aspect ratio, set DecodePixelWidth
// or DecodePixelHeight but not both.
myBitmapImage.DecodePixelWidth = 200;
myBitmapImage.EndInit();
//
//when you are ready to render the BitmapImage, do:
imageThumb.Source = myBitmapImage;
请注意 DecodePixelWidth 和 DecodePixelHeight 属性,以按所需的缩小像素大小缓存图像。使用两者来拉伸(stretch)图像以适合缩略图大小。
关于c# - 创建图像的缩略图并将它们存储在缓存中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4567328/