4

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

tabina
  • 1,095
  • 1
  • 14
  • 37
  • Have you removed your CacheOption? Get rid of that entire line; `bmp.CacheOption = BitmapCacheOption.OnLoad;` – Aaron McIver May 09 '12 at 15:19
  • If I did that, the second image acquisition would fail because it could not write its results to the file "camera_image.tif". – tabina May 09 '12 at 15:51
  • I want the user to be able to 1.) acquire an image, 2.) check if it is ok by displaying it to him, 3.) if ok: trigger a save to a different file location, 4.) if not ok: try again and acquire another image... – tabina May 09 '12 at 15:52
  • `OnLoad` closes the stream, `OnDemand`, the default behavior does not. – Aaron McIver May 10 '12 at 02:46
  • Apparently, I need to get the stream closed in order to get access to the file from somewhere else. Is there any way to tell the to release its cache? Or whatever else is neccessary to get the correct bitmap displayed? – tabina May 11 '12 at 09:54

1 Answers1

10

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!

Community
  • 1
  • 1
tabina
  • 1,095
  • 1
  • 14
  • 37