c# - 创建图像的缩略图并将它们存储在缓存中

标签 c# wpf image listbox

我有一个 WPF 应用程序,它有一个图像列表框。现在我正在使用 BitmapImageBitmapCacheOption.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/

相关文章:

c# - 如何在 C# 中获取来电显示?

c# - Linq - 高级 .OrderBy

arrays - 控制 PowerShell 中函数返回的类型

javascript - 创建一个非开发人员以后可以更改图像的 HTML/PHP 页面

linux - 如何使用 wget 从它的 src 抓取不同类型的图像?

javascript: 能否获取原图的宽度和高度?

c# - ASP.NET 母版页 DefaultButton 覆盖

c# - 为什么控制台应用程序比 C# 中的表单应用程序花费更多时间

c# - 具有 HierarchicalDataTemplate 和包含子项的 ObservableCollection 的 WPF TreeView

WPF 验证手动将错误添加到 Validation.Errors 集合中