image.jpg byte array to writeablebitmap through base64 WP8

1.6k Views Asked by At

I'm trying to convert a .jpg or .png file to a writeable bitmap. I obtain the image in another source and convert to a base64 encoding. After removing the packaging, I have a width, height, and base64 data. I then use:

var base64 = dataurl.Substring(dataurl.IndexOf("base64,") + 7);
binData = Convert.FromBase64String(base64);

This gives me the binary data of my image. The problem really comes in that I'm writing this for windows phone 8, so I'm limited in what libraries and methods I can use. The obvious choice is:

using (var stream = new MemoryStream(binData, 0, binDta.Length, true, true))
{
    var wbp = new WriteableBitmap(1,1).LoadJpeg(stream);
}

but I'm getting a System.ArgumentException from the WriteableBitmap library. Any ideas that work on Windows Phone 8?

1

There are 1 best solutions below

3
On

You should make sure the base64 decoded data is really a valid format like JPG and PNG. Then I'd recommend you try the WriteableBitmapEx FromStream method:

var wbp  = new WriteableBitmap(1, 1).FromStream(stream);