Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Image file locked after loading in WPF

I am reading my WPF imagesource like this:

VB

Dim bmi As BitmapImage = New BitmapImage
bmi.BeginInit
bmi.CacheOption = BitmapCacheOption.None
bmi.CreateOptions = BitmapCreateOptions.IgnoreImageCache
bmi.UriSource = New Uri(input.FullName, UriKind.Absolute)
bmi.EndInit

C#

BitmapImage bmi = new BitmapImage();
bmi.BeginInit();
bmi.CacheOption = BitmapCacheOption.None;
bmi.CreateOptions = BitmapCreateOptions.IgnoreImageCache;
bmi.UriSource = new Uri(input.FullName,  UriKind.Absolute);
bmi.EndInit();

It works like it should until this point. But user can update the image by copying the file. Then I want to refresh the image. But the file "MyFileName" is locked and when I want do overwrite it, it throws an error that it is already in use and locked.

But wait, I searched here for a solution and I got it:

bmi.cachoption = OnLoad

was the key... BUT!! now, the image is always the old one and is not updated to the new file. How to clear this cache?

In VB.Net I made an System.Drawing.Bitmap from stream. How to do it best way in WPF?

Regards

like image 309
goldengel Avatar asked Aug 19 '11 13:08

goldengel


1 Answers

dlev had a good advice. Here you see the cache option that should solve it: Problems overwriting (re-saving) image when it was set as image source

imgTemp.CreateOptions = BitmapCreateOptions.IgnoreImageCache;
like image 178
Nasenbaer Avatar answered Sep 22 '22 12:09

Nasenbaer