Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Image does not refresh in custom picture box

My custom picture box contains a scrollviewer and an image. A dependecy property Image of type string is used to set the 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;
  }
}

An image is acquired via frame grabber and stored to a given location with name "camera_image.tif". The Image property is set to this filename. When I start a new image acquisition, I set the Image property via binding to null and the picture box updates to show no image. When the image acquisition is done, I set it to the "camera_image.tif" again. The problem is that the new image never shows up. Instead it is always the first acquired image that is displayed within the picture box. When I check the image file, it contains the new content.

How could I get the picture box to refresh the image?

Regards,

tabina

like image 257
tabina Avatar asked Jan 16 '23 17:01

tabina


1 Answers

I found the answer here:

Reloading an image in wpf and here. WPF Image.Source caching too aggressively

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

is the solution I was looking for!

like image 121
tabina Avatar answered Jan 25 '23 23:01

tabina