1

I am developing a WPF application, a messenger client. The user should be able to change his avatar image. When he right-clicks his avatar, a open file dialog appears and there he can select the image he wants. After he has made his decision I delete his current avatar and copy the new one in the same place with the same name. The avatar image is a rectangle with an imagebrush fill. The problem is that the image does not update until I restart the application. Here is the piece of code used to load the new image.

if (x.ShowDialog() == true)
{
    ImageBrush img = new ImageBrush();
    BitmapImage bit = new BitmapImage();
    Bitmap bmp = new Bitmap(x.FileName);
    System.IO.File.Delete(System.IO.Directory.GetCurrentDirectory() + "/data/images/avatar/x.png");
    bmp.Save(System.IO.Directory.GetCurrentDirectory() + "/data/images/avatar/x.png", System.Drawing.Imaging.ImageFormat.Png);

    bit.BeginInit();
    bit.CacheOption = BitmapCacheOption.OnLoad;
    bit.UriSource = new Uri(@"pack://siteoforigin:,,,/data/images/avatar/x.png");
    bit.EndInit();
    img.ImageSource = bit;
    ProfileImage.Fill = img;
}

I hope you can help me solve this issue or offer me some alternatives. Thank you all!

SuicideSheep
  • 5,260
  • 19
  • 64
  • 117
tudor.gergely
  • 4,800
  • 1
  • 16
  • 22
  • Instead of using Pack URI. Try giving absolute image path to Uri constructor i.e. `new Uri(System.IO.Directory.GetCurrentDirectory() + "/data/images/avatar/x.png")`. – Rohit Vats May 14 '14 at 18:36
  • Moreover, why do you create an intermediate Bitmap from `x.Filename` which you save to a new file, instead of just copying the file? – Clemens May 14 '14 at 18:41
  • @Clemens When the BitmapCacheOption is set to OnLoad I can copy the image directly but it only affects the rectangle after I reopen the window. – tudor.gergely May 15 '14 at 04:05
  • @RohitVats I have tried that but it gives me the same result, the image in the rectangle doesn't change. – tudor.gergely May 15 '14 at 04:06
  • Perhaps it is a caching problem. Try to also set `bit.CreateOptions = BitmapCreateOptions.IgnoreImageCache;`. Alternatively you could manually open the file and assign a `FileStream` to `bit.StreamSource`. – Clemens May 15 '14 at 07:02
  • @Clemens Yep, that worked(the IgnoreImageCache part). Thanks a lot. Do you know any downsides to ignoring cache? will it affect the performance of the app? How can I repay you for your answer? – tudor.gergely May 15 '14 at 10:37
  • No downside in your case, because you are reading a local file. I'll provide an answer. You can accept it by checking the accept mark on the left side. – Clemens May 15 '14 at 10:40

1 Answers1

1

In order to make BitmapImage reload the image file (with identical file path), you could set the BitmapCreateOptions.IgnoreImageCache option:

bit.BeginInit();
bit.CreateOptions = BitmapCreateOptions.IgnoreImageCache;
bit.CacheOption = BitmapCacheOption.OnLoad;
bit.UriSource = new Uri(@"pack://siteoforigin:,,,/data/images/avatar/x.png");
bit.EndInit();
img.ImageSource = bit;
Clemens
  • 123,504
  • 12
  • 155
  • 268