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

时间:2012-05-09 15:15:10

标签: wpf image binding 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;
  }
}

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

如何让图片框刷新图像?

此致

tabina

1 个答案:

答案 0 :(得分:8)

我在这里找到了答案:

Reloading an image in wpf 和这里。 WPF Image.Source caching too aggressively

bmp.CacheOption = BitmapCacheOption.OnLoad;
bmp.CreateOptions = BitmapCreateOptions.IgnoreImageCache;

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