UWP BitmapImage SetSource from MemoryStream hangs

后端 未结 4 1023
春和景丽
春和景丽 2021-01-19 18:39

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.

4条回答
  •  轮回少年
    2021-01-19 19:17

    I use an extension method that I use into the converter:

        public static BitmapImage AsBitmapImage(this byte[] byteArray)
        {
            if (byteArray != null)
            {
                using (var stream = new InMemoryRandomAccessStream())
                {
                    stream.WriteAsync(byteArray.AsBuffer()).GetResults(); 
                              // I made this one synchronous on the UI thread;
                              // this is not a best practice.
                    var image = new BitmapImage();
                    stream.Seek(0);
                    image.SetSource(stream);
                    return image;
                }
            }
    
            return null;
        }
    

提交回复
热议问题