For my application, I am trying to convert an WriteableBitmap
to byte[]
to store in the database, and then from byte[]
to BitmapImage
to display back to the user.
My current methods that so far, produce no results:
public byte[] ConvertBitmapToByteArray(WriteableBitmap bitmap)
{
using (Stream stream = bitmap.PixelBuffer.AsStream())
using (MemoryStream memoryStream = new MemoryStream())
{
stream.CopyTo(memoryStream);
return memoryStream.ToArray();
}
}
To convert from byte array to BitmapImage
, I use:
using (InMemoryRandomAccessStream ms = new InMemoryRandomAccessStream())
{
using (DataWriter writer = new DataWriter(ms.GetOutputStreamAt(0)))
{
writer.WriteBytes((byte[])buffer);
writer.StoreAsync().GetResults();
}
var image = new BitmapImage();
image.SetSource(ms);
imageByteTest.Source = image;
}
There is good documentation for Silverlight applications I have found, but very little in the way for Windows Store Universal Runtime Applications. Where are these methods going wrong?