Search code examples
c#windows-store-appswinrt-xamlwriteablebitmap

WriteableBitmap to PNG


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();


Solution

  • It turned out that the BitmapEncoder expects a stream filled with a png data. Every example code I've got and found on the internet, requires either an existing PNG file or to creates one with a picker etc. And I simply want to save my WriteableBitmap to a byte array in PNG format.

    As a solution, I've made a 1x1 pixel PNG file and copied its bytes to a byte array. Than I can do some manipulations with this byte array and don't need to write/read anything from files.

    Here is the solution:

    static readonly byte[] PngBytes = {
        137, 80, 78, 71, 13, 10, 26, 10, 0, 0, 0, 13, 73, 72, 68, 82, 0, 0, 0, 1, 0, 0, 0, 1,
        8, 6, 0, 0, 0, 31, 21, 196, 137, 0, 0, 0, 1, 115, 82, 71, 66, 0, 174, 206, 28, 233, 0,
        0, 0, 4, 103, 65, 77, 65, 0, 0, 177, 143, 11, 252, 97, 5, 0, 0, 0, 9, 112, 72, 89, 115,
        0, 0, 14, 195, 0, 0, 14, 195, 1, 199, 111, 168, 100, 0, 0, 0, 24, 116, 69, 88, 116, 83,
        111, 102, 116, 119, 97, 114, 101, 0, 112, 97, 105, 110, 116, 46, 110, 101, 116, 32, 52,
        46, 48, 46, 51, 140, 230, 151, 80, 0, 0, 0, 13, 73, 68, 65, 84, 24, 87, 99, 248, 255, 
        255, 255, 127, 0, 9, 251, 3, 253, 5, 67, 69, 202, 0, 0, 0, 0, 73, 69, 78, 68, 174, 66,
        96, 130
    };
    
    public static async Task<byte[]> ConvertToPngBytes(this WriteableBitmap bitmap)
    {
        using (var memoryStream = new InMemoryRandomAccessStream())
        {
            await memoryStream.WriteAsync(PngBytes.AsBuffer());
            memoryStream.Seek(0);
    
            BitmapEncoder encode = await BitmapEncoder.CreateAsync(BitmapEncoder.PngEncoderId,
                memoryStream);
            byte[] buffer = bitmap.PixelBuffer.ToArray();
            encode.SetPixelData(BitmapPixelFormat.Bgra8, BitmapAlphaMode.Straight, 
                (uint)bitmap.PixelWidth, (uint)bitmap.PixelHeight, 96.0, 96.0, buffer);
            await encode.FlushAsync();
            var stream = memoryStream.AsStream();
            stream.Seek(0, SeekOrigin.Begin);
            byte[] result = new byte[stream.Length];
            stream.Read(result, 0, result.Length);
            return result;
        }
    }