How to Convert a WriteableBitmap image to Byte array in WinRt App

c#, image-processing, windows-runtime

Solution

`WriteableBitmap` exposes `PixelBuffer` property of type `IBuffer` - a Windows Runtime interface which can be converted to a byte array with .NET `Stream`s

    byte[] ConvertBitmapToByteArray(WriteableBitmap bitmap)
    {
        using (Stream stream = bitmap.PixelBuffer.AsStream())
        using (MemoryStream memoryStream = new MemoryStream())
        {
            stream.CopyTo(memoryStream);
            return memoryStream.ToArray();
        }
    }

Problem

I want to convert a `WriteableBitmap` image to a `Byte[]` array using C# code in Windows store metro style apps.

Original source