WPF图像缓存
我有一个WPF应用程序,可以从视频文件中获取快照图像。用户可以定义拍摄图像的时间戳。然后将图像保存到磁盘上的临时位置,然后渲染到WPF图像缓存,wpf,image,Wpf,Image,我有一个WPF应用程序,可以从视频文件中获取快照图像。用户可以定义拍摄图像的时间戳。然后将图像保存到磁盘上的临时位置,然后渲染到元素中 然后,用户应该能够选择不同的时间戳,然后覆盖磁盘上的临时文件-这应该显示在元素中 使用Image.Source=null,我可以从元素中清除图像文件,因此它会显示一个空格。但是,如果源图像文件随后被新图像(同名)覆盖并加载到元素中,则仍显示旧图像 我使用以下逻辑: // Overwrite temporary file file here // Clear o
元素中
然后,用户应该能够选择不同的时间戳,然后覆盖磁盘上的临时文件-这应该显示在
元素中
使用Image.Source=null
,我可以从
元素中清除图像文件,因此它会显示一个空格。但是,如果源图像文件随后被新图像(同名)覆盖并加载到
元素中,则仍显示旧图像
我使用以下逻辑:
// Overwrite temporary file file here
// Clear out the reference to the temporary image
Image_Preview.Source = null;
// Load in new image (same source file name)
Image = new BitmapImage();
Image.BeginInit();
Image.CacheOption = BitmapCacheOption.OnLoad;
Image.UriSource = new Uri(file);
Image.EndInit();
Image_Preview.Source = Image;
即使原始文件已完全替换,在
元素中显示的图像也不会更改。是否存在我不知道的图像缓存问题?默认情况下,WPF缓存从URI加载的位图图像
您可以通过设置BitmapCreateOptions.IgnoreImageCache
标志来避免这种情况:
var image = new BitmapImage();
image.BeginInit();
image.CreateOptions = BitmapCreateOptions.IgnoreImageCache;
image.CacheOption = BitmapCacheOption.OnLoad;
image.UriSource = new Uri(file);
image.EndInit();
Image_Preview.Source = image;
或者直接从流加载位图图像:
var image = new BitmapImage();
using (var stream = new FileStream(file, FileMode.Open, FileAccess.Read))
{
image.BeginInit();
image.CacheOption = BitmapCacheOption.OnLoad;
image.StreamSource = stream;
image.EndInit();
}
Image_Preview.Source = image;