【问题标题】:Using Image Source with big images in WPF在 WPF 中使用带有大图像的图像源
【发布时间】: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 不足而无法加载图像,则可能无法轻易检测到。能够找到解决方法仍然很好......

标签: c# wpf xaml wic


【解决方案1】:

我使用 DecodePixelWidth 与在 XAML 上设置 Source 做了一个简单的测试(一个图像),使用 DecodePixelWidth 加载需要 28MB 和 178MB 而没有缩小。我很确定它不会将原始图像保留在内存中。

既然你说你正在处理多个图像,我怀疑这是一个图像重用问题。默认情况下,WPF 将缓存一个 BitmapImage 对象(无论是由代码创建还是在 XAML 中创建)。它查看 SourceUri 以及 DecodePixelWidth 和 DecodePixelHeight 以找到匹配项。如果您的 TargetWidth 和 TargetHeight 发生变化,则意味着 WPF 无法重用其图像缓存;如果您在没有任何额外选项的情况下设置源,则不会有问题。

【讨论】:

  • 你如何检查内存消耗(我只是在任务管理器中检查私有工作集)?哪个版本的WPF(我想我用的是3.51,但现在无法访问机器)。
  • 我正在以同样的方式检查内存。我也在使用 3.5 SP1。
【解决方案2】:

我也遇到了同样的问题,看起来当 BitmapImage 属性 CreateOptions = 设置为 BitmapCreateOptions.IgnoreColorProfile 时它的工作速度更快。

我们可以为经常使用的 BitmapImages 创建缓存的其他东西。我知道 WPF 应该自动执行此操作,但我认为它会运行得更快。如果有人会尝试测量加载时间,只需编写 cmets :)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-05-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多