0

我有一个返回的函数BitmapImage

    private BitmapImage dfa2bmp(DFA dfa)
    {
        //[...]
        //myGraph.png generated here[...]
        BitmapImage bmp = new BitmapImage();
        bmp.BeginInit();
        bmp.CacheOption = BitmapCacheOption.OnLoad;
        bmp.UriSource = new Uri(graphvizDir + "\\myGraph.png");
        bmp.EndInit();
        File.Delete(graphvizDir + "\\myGraph.png");

        return bmp;
    }

所以每次调用后,文件 myGraph.png 都不同。但是,当我将返回值分配给dfa2bmpImage 控件时,我仍然得到旧图像,即使它在上次调用我的函数时被删除。

我究竟做错了什么?

4

2 回答 2

0

您需要将 设置BitmapCreateOptionsIgnoreImageCache

  bmp.CreateOptions = BitmapCreateOptions.IgnoreImageCache;

BitmapCreateOptions.IgnoreImageCache当您有需要刷新的图像时使用

    // Summary:
    //     Loads images without using an existing image cache. This option should only
    //     be selected when images in a cache need to be refreshed.
    IgnoreImageCache = 8,
于 2013-05-16T00:24:27.280 回答
0

bmp.CacheOption = BitmapCacheOption.OnLoad;此行将图像缓存在 RAM 中,然后从那里显示。文档说;

在加载时将整个图像缓存到内存中。所有对图像数据的请求都从内存存储中填充。

如果您想阻止程序在删除后使用图像,请将 BitmapCacheOption 更改为 none。所以; bmp.CacheOption = BitmapCacheOption.None;会做你想做的。

编辑:我不知道这会有什么副作用,我并没有真正处理这些库,但是当您尝试将已删除的图像分配给图像控件时,我看到了一些异常的可能性。我认为更好的解决方案可能是设置null将导致 GC 释放包含图像的内存的对象。

于 2013-05-15T23:29:19.143 回答