在WPF中重新加载图像

10

我正在尝试重新加载在WPF中显示的图像(System.Windows.Controls.Image)。我是这样设置源的:

ScreenAtco01Image.Source = new BitmapImage(new Uri(@"Y:/screenshots/naratco08-0-0-screenshot.png", UriKind.RelativeOrAbsolute));

我做了一个按钮,它应该强制重新加载这张图片(每秒钟在磁盘上更改)。

我尝试重置源,但什么也没发生。但是,如果我将源更改为不同的图像,则会加载该不同的图像。看起来好像有些东西被缓存了?

谢谢你的帮助。

1个回答

27

我找到了一个适合我的答案:

BitmapImage _image = new BitmapImage();
_image.BeginInit();
_image.CacheOption = BitmapCacheOption.None;
_image.UriCachePolicy = new RequestCachePolicy(RequestCacheLevel.BypassCache);
_image.CacheOption = BitmapCacheOption.OnLoad;
_image.CreateOptions = BitmapCreateOptions.IgnoreImageCache;
_image.UriSource = new Uri(@"Y:/screenshots/naratco08-0-0-screenshot.png", UriKind.RelativeOrAbsolute);
_image.EndInit();
ScreenAtco01Image.Source = _image;

非常感谢!我自己一直在努力解决这个问题,你的回答让我受益匪浅。如果可以我会投更多票的。 - Niklas Winde

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接