Q: How can I stop BitmapImage, in .NET Windows Presentation Foundation (WPF) from locking the source image file?
A: By default BitmapInfo seems to lock the source image file so that you can use the bitmapinfo object and modify or delete the source file at the same (or similar) time. For example, this C# will probably yield a locked image file:
var bitmap = new BitmapImage(new Uri(imageFilePath));
// use bitmap...
You can get around this locking problem as follows, it’s a little bit more long-winded but it worked for us….
var bitmap = new BitmapImage();
var stream = File.OpenRead(imageFilePath);
bitmap.CacheOption = BitmapCacheOption.OnLoad;
bitmap.StreamSource = stream;
// Use bitmap.....