图像不会在自定义图片框中刷新

tab*_*ina 4 wpf binding image bitmap

我的自定义图片框包含滚动查看器和图像.依赖属性string类型的图像用于设置图像.

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;
  }
}
Run Code Online (Sandbox Code Playgroud)

通过帧抓取器获取图像并将其存储到名为"camera_image.tif"的给定位置.Image属性设置为此文件名.当我开始新的图像采集时,我通过绑定将Image属性设置为null,图片框更新以显示没有图像.完成图像采集后,我再次将其设置为"camera_image.tif".问题是新图像永远不会出现.相反,它始终是图片框中显示的第一个获取的图像.当我检查图像文件时,它包含新内容.

我怎么能让图片框刷新图像?

问候,

tabina

tab*_*ina 9

我在这里找到了答案:

在wpf 和这里重新加载图像. WPF Image.Source缓存过于激烈

bmp.CacheOption = BitmapCacheOption.OnLoad;
bmp.CreateOptions = BitmapCreateOptions.IgnoreImageCache;
Run Code Online (Sandbox Code Playgroud)

是我正在寻找的解决方案!