Load bitmapImage from base64String

后端 未结 2 768
無奈伤痛
無奈伤痛 2021-01-14 05:31

How can I load a bitmapImage from base64String in windows 8?

I tried this but I am not successful. It used to work on windows

2条回答
  •  一生所求
    2021-01-14 05:41

    To create an IRandomAccessStream object for the SetSource method, you need to use a DataWriter. Take a look to this code:

        public async Task GetImage(string value)
        {
            if (value == null)
                return null;
    
            var buffer = System.Convert.FromBase64String(value);
            using (InMemoryRandomAccessStream ms = new InMemoryRandomAccessStream())
            {
                using (DataWriter writer = new DataWriter(ms.GetOutputStreamAt(0)))
                {
                    writer.WriteBytes(buffer);
                    await writer.StoreAsync();
                }
    
                var image = new BitmapImage();
                image.SetSource(ms);
                return image;
            }
        }
    

提交回复
热议问题