Load bitmapImage from base64String
bitmapimage, c#, microsoft-metro, windows-8
Solution
To create an IRandomAccessStream object for the SetSource method, you need to use a DataWriter. Take a look to this code:
public async Task<BitmapImage> 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;
}
}
Problem
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 phone. What is different? Looks like I have to use the function setsourceasync. When I use that, then I am required to pass the parameter as IRandomMemory which I am unable to do. How to do this? ``` public static BitmapImage Base64ToImage(string base64String) { var bitmapImage = new BitmapImage(); try { if (!String.IsNullOrEmpty(base64String)) { var imageBytes = Convert.FromBase64String(base64String); using (var ms = new MemoryStream(imageBytes, 0, imageBytes.Length)) { bitmapImage.SetSourcec(ms); return bitmapImage; } } } catch (Exception e) { } return null; } ```