【问题标题】:tiff viewer wpf application out of memorytiff 查看器 wpf 应用程序内存不足
【发布时间】:2016-03-14 13:08:33
【问题描述】:

我正在开发一个处理大型 24 位彩色 tif 文件 (>70MB) 的 Tiff Viewer 项目。 这是我如何加载 tif 文件的代码:

TiffBitmapDecoder tbd = new TiffBitmapDecoder(new Uri(_strTiffPath),BitmapCreateOptions.DelayCreation, BitmapCacheOption.Default);
_frames = tbd.Frames;

我使用默认缓存选项来防止将整个文件加载到内存中。

我的应用程序有一个侧面缩略图视图(带有图像的垂直 StackPanel)和一个查看所选缩略图的页面视图。

我通过这段代码只加载可见的缩略图:

internal static BitmapSource GetThumbAt(int i)
{
    try
    {
        if (i >= _frames.Count)
            return null;

        BitmapFrame bf = _frames[i];
        bf.Freeze();
        return bf;
    }
    catch (Exception ex)
    {
        return null;
    }
}

我的问题是当我向下滚动缩略图视图以加载新的可见页面时,内存负载增加并且内存不足!

我试图卸载不可见的页面(已经加载),但这没有帮助!

img.Source = null

谢谢你帮我解决这个问题。

【问题讨论】:

  • 我找到了一篇文章here 讲这个问题,但是不知道如何适配自己的代码。有什么想法吗?

标签: wpf memory thumbnails tiff out


【解决方案1】:

我想通了! 正如我之前的评论中提到的,this article 对我帮助很大。 我刚刚将它调整为我的代码,内存现在正在正确卸载。 以下是我对代码所做的修改:

internal static BitmapSource GetThumbAt(int i) 
{
    try
    {
        if (i >= _frames.Count)
            return null;

        BitmapFrame bf = _frames[i];
        BitmapSource bs = bf.Clone() as BitmapSource; //make a copy of the original because bf is frozen and can't take any new property
        BitmapUtility.AddMemoryPressure(bs);
        return bs;
    }
    catch (Exception ex)
    {
        return null;
    }
}

【讨论】:

    猜你喜欢
    • 2011-02-15
    • 2013-09-15
    • 1970-01-01
    • 1970-01-01
    • 2012-07-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多