.net - none - wpf image source null

BitmapImage in WPF does lock file (3)

I use:

Dim bmi As New BitmapImage(New Uri(fiInfo.FullName, UriKind.Absolute))
bmi.CacheOption = BitmapCacheOption.OnLoad

this does not Use OnLoad And file still is locked to overwrite on harddisk. Any idea how to unlock?


As shown in the question you link to, you'd need to call BeginInit and EndInit, like so as well as set the UriSource property:

Dim bmi As New BitmapImage()
bmi.CacheOption = BitmapCacheOption.OnLoad
bmi.UriSource = New Uri(fiInfo.FullName, UriKind.Absolute)

I had a similar problem and I solved using this method: (it's a personalization of an answer here)

    public static BitmapImage BitmapFromUri(Uri source)
        var bitmap = new BitmapImage();
        bitmap.UriSource = source;
        bitmap.CacheOption = BitmapCacheOption.OnLoad;
        return bitmap;

You can open the image like this:

BitmapImage bimg = BitmapFromUri(new Uri(some_URI));

And it releases the image immediatly after loading it.

Hope it can helps!

BitmapFrame.Create(new Uri(filePath), BitmapCreateOptions.None, BitmapCacheOption.OnLoad);