julienduchow
julienduchow

Reputation: 1076

Image.Source property (via BitmapImage) lockes the file

i want to set the image.source property in my code, like this:

Dim bi as new BitmapImage

bi.BeginInit()
bi.UriSource = new Uri("C:/test.png", uriKind.relativeOrAbsolute)
bi.EndInit()

img.Source = bi

Thats working, but the problem is that this locked the file and makes me unable to replace the file.

Now my question: Is there any other method to set the image.source property, so that i can say something like file.close() after it?

Upvotes: 0

Views: 60

Answers (1)

Anton Danylov
Anton Danylov

Reputation: 1491

Specify BitmapCacheOption.OnLoad to your BitmapImage:

Dim bi as new BitmapImage

bi.BeginInit()
bi.CacheOption = BitmapCacheOption.OnLoad;
bi.UriSource = new Uri("C:/test.png", uriKind.relativeOrAbsolute)
bi.EndInit()

img.Source = bi

Or you may read image file contents to the buffer in memory and then use MemoryStream as a StreamSource for the BitmapImage:

var imageBytes = File.ReadAllBytes("C:/test.png")
var stream = new MemoryStream(imageBytes);
var bi = new BitmapImage();

bi.BeginInit();
bi.StreamSource = stream;
bi.EndInit();

Upvotes: 1

Related Questions