In my UWP app i store images in an SQLite db in form of byte[]. Then as i retrieve my objects from the db i bind them to a GridView data template which has an Image control. As i cant bind the Image's Source directly to the array, so i have created a BitmapImage property in my object's class to bind the Image control to:
public BitmapImage Icon
{
get
{
using (var stream = new MemoryStream(icon))
{
stream.Seek(0, SeekOrigin.Begin);
var img = new BitmapImage();
img.SetSource(stream.AsRandomAccessStream());
return img;
}
}
}
The problem is, my app hangs on the img.SetSource line. After some experimenting, i have found that this problem can be overcome with a second MemoryStream:
public BitmapImage Icon
{
get
{
using (var stream = new MemoryStream(icon))
{
stream.Seek(0, SeekOrigin.Begin);
var s2 = new MemoryStream();
stream.CopyTo(s2);
s2.Position = 0;
var img = new BitmapImage();
img.SetSource(s2.AsRandomAccessStream());
s2.Dispose();
return img;
}
}
}
For some reason it works, does not hang. I wonder why? And how to deal with this situation properly? Thanks!