0

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 Answers1

1

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);
Rene Schulte
  • 2,962
  • 1
  • 19
  • 26
  • The base 64 encoding comes from a javascript line `var data = canvas.toDataURL();` I've tried sending the canvas data from jpeg,png,and bmp images, but they always pop out the other side with image/jpg. I would consider this a valid format, and I've tried .FromStream(stream). I'll still consider your advice and stop trying to use .FromByteArray and .FromJpeg. Any more tips? I can give you the binData or base64 data from either .png or .jpg files if that helps. – rickstockham Jan 25 '13 at 16:42
  • You should just use the bin data and save it as a jpeg/ png file and see if Windows can open it. Just to make sure it's a valid file format. – Rene Schulte Jan 28 '13 at 08:19
  • 1
    Figured it out, and your answers were all close. Because of the 'special' nature of windows phone 8, and the compressed nature of the byte data, `writeableBitmap.LoadJpeg(stream);` was the correct choice, inside of a `Deployment.Curretn.Dispatcher.BeginInvoke();` Oh the joys of thread dependency. I'm pretty sure this is impossible with any libraries I know for png. Lucky I can force all my images to be jpg. – rickstockham Jan 30 '13 at 00:40