【问题标题】:How to load a very large sourceimage in WPF image?如何在 WPF 图像中加载非常大的源图像?
【发布时间】:2026-02-06 04:10:02
【问题描述】:

我有一个非常大的图像 (600mb) 30000x30000,想将它加载到 wpf 图像控件中。

我可以使用 Windows 照片查看器观看此图像!

我将我的 testapp 设置为 64 位并使用以下代码。

var image = new BitmapImage();
image.BeginInit();

// load into memory and unlock file
image.CacheOption = BitmapCacheOption.OnLoad;

image.UriSource = uri;

image.EndInit();

imagecontrol.source = image;

测试应用仅显示带有此大图像的白屏。

像 100mb 和 7000x7000 这样的较小的也可以。

我做错了什么?抱歉我的英语不好,提前感谢。

【问题讨论】:

    标签: wpf image view load


    【解决方案1】:

    64-Bit Applications.

    与 32 位 Windows 操作系统一样,在 64 位 Windows 操作系统上运行 64 位托管应用程序时,您可以创建的对象大小限制为 2GB。

    【讨论】:

    • 有确切的问题。 400mb tif 图像作为图像源。 Wpf 想出了一个白屏。未检测到内存使用情况。通过编译针对特定 x64 平台的应用程序修复了该问题。但为什么?我还是不明白。为什么?
    【解决方案2】:

    我会将它分成 10 个 (3000x3000) 段并将它们放入 10 个文件中。

    还要检查您使用的是什么格式。它可能正在填充文件大小或特定格式的阈值。尝试 TIF 格式,然后尝试 JPG,然后尝试 BMP 等。还看看您是否可以将 JPG 格式压缩到 40-50%,看看是否有任何变化。

    告诉我你发现了什么。

    【讨论】:

    • 感谢您的快速回答,明天将尝试。我必须在图像中进行测量,所以对我来说最简单的方法是将其作为一个文件加载。
    • 我尝试了其他格式,但遗憾的是收效甚微。
    • 也不是 :(。但是感谢您的努力。mb 有人想出了一个解决方案。根据link,位图源的最大大小是 64gb...,但是 mb wpf 图像确实不支持这个尺寸...
    【解决方案3】:

    您可以直接从硬盘显示图片以减少内存使用。

    Сheck BitmapCacheOption

    此代码示例将直接从硬盘读取图像并显示小缩略图。

    public BitmapImage memoryOptimizedBitmapRead(string url,FrameworkElement imageContainer)
            {
                if (string.IsNullOrEmpty(url) || imageContainer.ActualHeight<= 0)
                {
                    return null;
                }
    
                var bi = new BitmapImage();
                bi.BeginInit();
    
                bi.CacheOption = BitmapCacheOption.None;
                bi.CreateOptions = BitmapCreateOptions.IgnoreColorProfile;
                bi.DecodePixelHeight = (int)imageContainer.ActualHeight;
                bi.UriSource = new Uri(url, UriKind.Absolute);
    
                // End initialization.
                bi.EndInit();
                bi.Freeze();
                return bi;
    
            }
    

    最大缩略图大小由图像容器大小决定。

    方法使用示例:

    var image= new Image();
    image.Source = memoryOptimizedBitmapRead(...);
    

    【讨论】: