You have to set BitmapCacheOptions.OnLoad
when you want to close the stream directly after initializing the BitmapImage:
var userImage = new BitmapImage();
using (var stream = new MemoryStream(...))
{
userImage.BeginInit();
userImage.CacheOption = BitmapCacheOption.OnLoad;
userImage.StreamSource = stream;
userImage.EndInit();
}
UserImage = userImage;
See the Remarks section in BitmapImage.CacheOption:
Set the CacheOption to BitmapCacheOption.OnLoad if you wish to close a stream used to create the BitmapImage. ...