BitmapCacheOption.OnLoad 仅显示白色图像

BitmapCacheOption.OnLoad show only white Image

我使用以下代码加载图像(一次加载多个 windows):

BitmapImage tempBitmapImage = new BitmapImage();
tempBitmapImage.BeginInit();
tempBitmapImage.UriSource = new Uri(_fileList[_fileCounter].FileName);
tempBitmapImage.CacheOption = BitmapCacheOption.Default; //.OnLoad;
tempBitmapImage.EndInit();
tempImage.Source = tempBitmapImage;

如果我使用默认选项,图像会加载并显示,但当多个 windows 尝试加载文件时应用程序可能会崩溃,然后文件会被 运行 的更新程序删除在后台。如果我使用 OnLoad 选项,当我将图像从一个图像更改为另一个图像时,文件应该完全在内存中。但是当我这样做的时候,只有一个白色的图像,没有错误信息,没有颜色,只有一个白色的屏幕。

有人知道那是什么吗?

您可以直接从 FileStream 加载 BitmapImage:

var tempBitmapImage = new BitmapImage();

using (var stream = new FileStream(
    _fileList[_fileCounter].FileName, FileMode.Open, FileAccess.Read))
{
    tempBitmapImage.BeginInit();
    tempBitmapImage.CacheOption = BitmapCacheOption.OnLoad;
    tempBitmapImage.StreamSource = stream;
    tempBitmapImage.EndInit();
}

tempImage.Source = tempBitmapImage;