I'm writing a simple WindowsStore app and I can not find out how can I save a WriteableBitmap
to some memory stream in PNG image format in order to get the bytes of this PNG image.
What I can find so far is how to do that on WP7.1 but it doesn't work.
Can anyone share a snippet or point to useful resources?
UPDATE:
I use WriteableBitmap
to draw on it.
I have this extension method.
public static async Task<byte[]> ConvertToPngBytes(this WriteableBitmap bitmap)
{
using (IRandomAccessStream memoryStream = new InMemoryRandomAccessStream())
{
BitmapEncoder encode = await BitmapEncoder.CreateAsync(BitmapEncoder.PngEncoderId, memoryStream);
byte[] buf = bitmap.PixelBuffer.ToArray();
encode.SetPixelData(BitmapPixelFormat.Bgra8, BitmapAlphaMode.Straight, (uint)bitmap.PixelWidth, (uint)bitmap.PixelHeight, 96.0, 96.0, buf);
await encode.FlushAsync();
await memoryStream.FlushAsync();
var stream = memoryStream.AsStream();
byte[] result = new byte[stream.Length];
stream.Read(result, 0, result.Length);
return result;
}
}
It hangs on await encode.FlushAsync();