【发布时间】:2010-03-23 23:21:07
【问题描述】:
我正在开发一个允许用户使用 ItemsControl 操作多个图像的应用程序。我开始运行一些测试,发现该应用程序在显示一些大图像时出现问题 - 即。它不适用于高分辨率 (21600x10800)、20MB 图像 http://earthobservatory.nasa.gov/Features/BlueMarble/BlueMarble_monthlies.php,虽然它显示来自http://zebu.uoregon.edu/hudf/hudf.jpg 的 6200x6200、60MB 哈勃望远镜图像就好了。
最初的解决方案只是指定了一个带有 Source 属性的 Image 控件,该属性指向磁盘上的一个文件(通过绑定)。使用 Blue Marble 文件 - 图像不会显示。现在这可能只是一个隐藏在时髦的 MVVM + XAML 实现中某个深处的错误 - Snoop 显示的可视化树如下所示:
Window/Border/AdornerDecorator/ContentPresenter/Grid/Canvas/UserControl/Border/ContentPresenter/Grid/Grid/Grid/Grid/Border/Grid/ContentPresenter/UserControl/UserControl/Border/ContentPresenter/Grid/Grid/Grid/Grid /Viewbox/ContainerVisual/UserControl/Border/ContentPresenter/Grid/Grid/ItemsControl/Border/ItemsPresenter/Canvas/ContentPresenter/Grid/Grid/ContentPresenter/Image...
现在调试这个! WPF 可以这么疯狂...
无论如何,事实证明,如果我创建一个简单的 WPF 应用程序 - 图像加载得很好。我试图找出根本原因,但我不想花几周的时间。我认为正确的做法可能是使用转换器来缩小图像 - 这就是我所做的:
ImagePath = @"F:\Astronomical\world.200402.3x21600x10800.jpg";
TargetWidth = 2800;
TargetHeight = 1866;
和
<Image>
<Image.Source>
<MultiBinding Converter="{StaticResource imageResizingConverter}">
<MultiBinding.Bindings>
<Binding Path="ImagePath"/>
<Binding RelativeSource="{RelativeSource Self}" />
<Binding Path="TargetWidth"/>
<Binding Path="TargetHeight"/>
</MultiBinding.Bindings>
</MultiBinding>
</Image.Source>
</Image>
和
public class ImageResizingConverter : MarkupExtension, IMultiValueConverter
{
public Image TargetImage { get; set; }
public string SourcePath { get; set; }
public int DecodeWidth { get; set; }
public int DecodeHeight { get; set; }
public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
{
this.SourcePath = values[0].ToString();
this.TargetImage = (Image)values[1];
this.DecodeWidth = (int)values[2];
this.DecodeHeight = (int)values[3];
return DecodeImage();
}
private BitmapImage DecodeImage()
{
BitmapImage bi = new BitmapImage();
bi.BeginInit();
bi.DecodePixelWidth = (int)DecodeWidth;
bi.DecodePixelHeight = (int)DecodeHeight;
bi.UriSource = new Uri(SourcePath);
bi.EndInit();
return bi;
}
public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
{
throw new Exception("The method or operation is not implemented.");
}
public override object ProvideValue(IServiceProvider serviceProvider)
{
return this;
}
}
现在这工作正常,除了一个“小”问题。当您仅在 Image.Source 中指定文件路径时 - 应用程序实际上使用更少的内存并且比使用 BitmapImage.DecodePixelWidth 时运行得更快。如果您有多个指向同一图像的图像控件,加上 Image.Source - 它们只使用与仅加载一个图像一样多的内存。使用 BitmapImage.DecodePixelWidth 解决方案 - 每个额外的 Image 控件都使用更多内存,并且每个控件都比仅指定 Image.Source 时使用更多。也许 WPF 以某种方式以压缩形式缓存这些图像,而如果您指定解码的尺寸 - 感觉就像您在内存中获得了未压缩的图像,加上它需要 6 倍的时间(也许没有它,缩放是在 GPU 上完成的?),加上感觉就像原始的高分辨率图像也被加载并占用了空间。
如果我只是缩小图像,将其保存到一个临时文件,然后使用 Image.Source 指向该文件 - 它可能会工作,但它会很慢,并且需要处理临时文件的清理.如果我可以检测到未正确加载的图像 - 也许我只能在需要时将其缩小,但 Image.ImageFailed 永远不会被触发。也许它与视频内存有关,而这个应用程序只是将更多的内存与深度视觉树、不透明蒙版等一起使用。
实际问题:我如何才能像 Image.Source 选项一样快速加载大图像,而无需使用更多内存来存储额外的副本,并且如果我只需要低于原始分辨率的特定分辨率的缩小图像使用额外的内存?另外,如果没有图像控件不再使用它们,我不想将它们保留在内存中。
【问题讨论】:
-
我意识到 21600x10800=233280000 像素是一个相当大的图像,如果将 890MB 的未压缩 RGBA 数据作为一个整体保存在内存中。内存不足问题通常无法通过代码很好地处理,因此如果 WPF 由于可用 RAM 不足而无法加载图像,则可能无法轻易检测到。能够找到解决方法仍然很好......