我的自定义图片框包含一个滚动视图和一张图片。 使用类型为字符串的依赖属性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属性为该文件名。当开始新的图像采集时,通过绑定将Image属性设置为空,并更新图像框以显示无图像。当图像采集完成时,再次将其设置为“camera_image.tif”。问题是新图像从未显示过,而是始终显示第一个采集到的图像。当检查图像文件时,它包含了新内容。如何刷新图像框以显示新图像?
bmp.CacheOption = BitmapCacheOption.OnLoad;
- Aaron McIverOnLoad
会关闭流,而OnDemand
则不会,这是默认行为。 - Aaron McIver