The following code is used in a loop to load images into a XAML Listbox:
Dim image As New Image
Dim uri As New Uri(filePathAndName)
image.Source = New BitmapImage(uri)
Another post comments in the answer that the BitmapImage class hangs on to the file handle which is what I observe.
Various other posts gave examples of how to load the images without getting a file lock. This is the one I used:
Dim image As New Image
Using stream = New FileStream(filePathAndName, FileMode.Open)
Dim bitmapImage As New BitmapImage
bitmapImage.BeginInit()
bitmapImage.CacheOption = BitmapCacheOption.OnLoad
bitmapImage.StreamSource = stream
bitmapImage.EndInit()
image.Source = bitmapImage
End Using
The second coding loads the images without file locks, but it is much slower.
Are there no ways to force the file lock off using bitmapImage in the first set of code?