UWP BitmapImage SetSource from MemoryStream hangs

后端 未结 4 1018
春和景丽
春和景丽 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:05

    I've had an issues printing images loading from RandomAccessStreams until I found this. They load fine in the app for visual but would hang the UI when generating print previews dynamically.

    Conversion works perfectly, cheers.

    private BitmapImage ConvertImage(string str) {
    byte[] imgData = Convert.FromBase64String(str);
    using (InMemoryRandomAccessStream ms = new InMemoryRandomAccessStream())
    {
        using (DataWriter writer = new DataWriter(ms.GetOutputStreamAt(0)))
        {
            writer.WriteBytes(imgData);
            writer.StoreAsync.GetResults();
        }
        BitmapImage result = new BitmapImage();
        result.SetSource(ms);
        return result;
    }}
    

提交回复
热议问题