WPF图像缓存 [英] WPF Image Caching

查看:988
本文介绍了WPF图像缓存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个WPF应用程序,它从视频文件中获取快照图像。用户可以定义从中获取图像的时间戳。然后将图像保存到磁盘上的临时位置,然后将其渲染为< image> 元素。



<然后,用户应该能够选择不同的时间戳,然后覆盖磁盘上的临时文件 - 然后应该在< image> 元素中显示。



使用 Image.Source = null; ,我可以清除图像文件< image> 元素,因此它会显示一个空格。但是,如果源图像文件随后被新图像(具有相同名称)覆盖并加载到< image> 元素中,则仍会显示旧图像



我使用以下逻辑:

  //在此覆盖临时文件文件

//清除对临时图像的引用
Image_Preview.Source = null;

//加载新图像(相同的源文件名)
Image = new BitmapImage();
Image.BeginInit();
Image.CacheOption = BitmapCacheOption.OnLoad;
Image.UriSource = new Uri(file);
Image.EndInit();
Image_Preview.Source = Image;

< image> 元素不会更改,即使原始文件已被完全替换。这里是否存在我不知道的图像缓存问题?

解决方案

默认情况下,WPF缓存从URI加载的BitmapImages 。



你可以通过设置 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;

或者你直接从Stream加载BitmapImage:

  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;


I have a WPF application that takes a snapshot image from a video file. The user can define the timestamp from which to take the image. The image is then saved to a temporary location on disk, and is then rendered into an <image> element.

The user should then be able to select a different timestamp, which then overwrites the temporary file on disk - this should then be displayed within the <image> element.

Using Image.Source = null;, I can clear the image file from the <image> element, so it displays a blank space instead. However, if the source image file is then overwritten with a new image (with the same name) and loaded into the <image> element, it still shows the old image.

I am using the following logic:

// 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;

The image displayed in the <image> element does not change, even though the original file has been completely replaced. Is there an image caching issue here that I am not aware of?

解决方案

By default, WPF caches BitmapImages that are loaded from URIs.

You can avoid that by setting the BitmapCreateOptions.IgnoreImageCache flag:

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;

Or you load the BitmapImage directly from a Stream:

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;

这篇关于WPF图像缓存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆