我的自定义图片框包含一个滚动查看器和一个图像。 字符串类型的依赖属性 Image 用于设置图像。
public static DependencyProperty ImageProperty = DependencyProperty.Register(
"Image", typeof(string), typeof(CustomPictureBox), new FrameworkPropertyMetadata("", new PropertyChangedCallback(OnImageChanged)));
private static void OnImageChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
CustomPictureBox cpb = (CustomPictureBox)d;
if (e.Property == ImageProperty)
{
string newvalue = e.NewValue as string;
if (!(string.IsNullOrEmpty(newvalue)))
{
var bmp = new BitmapImage();
bmp.BeginInit();
bmp.UriSource = new Uri(newvalue);
bmp.CacheOption = BitmapCacheOption.OnLoad;
bmp.EndInit();
cpb.imgPicture.Source = bmp;
}
else
cpb.imgPicture.Source = null;
}
}
图像通过图像采集卡获取并存储到名为“camera_image.tif”的给定位置。 Image 属性设置为此文件名。 当我开始新的图像采集时,我通过绑定(bind)将 Image 属性设置为 null,并且图片框更新为不显示图像。 图像采集完成后,我再次将其设置为“camera_image.tif”。问题是新图像永远不会显示。相反,它始终是显示在图片框中的第一个获取的图像。当我检查图像文件时,它包含新内容。
如何让图片框刷新图像?
问候,
塔宾娜
最佳答案
我在这里找到了答案:
Reloading an image in wpf 和这里。 WPF Image.Source caching too aggressively
bmp.CacheOption = BitmapCacheOption.OnLoad;
bmp.CreateOptions = BitmapCreateOptions.IgnoreImageCache;
这是我一直在寻找的解决方案!
关于wpf - 自定义图片框中的图像不刷新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10518986/