Image file locked after loading in WPF
Asked Answered
C

1

6

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

Crookback answered 19/8, 2011 at 13:5 Comment(1)
see if #2097652 helps? i don't know how to clear the cache explicity, but this may help you anyway.Shoebill
I
9

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;
Inflationary answered 19/8, 2011 at 13:18 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.