6

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

1 Answer 1

8

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

    byte[] ConvertBitmapToByteArray(WriteableBitmap bitmap)
    {
        using (Stream stream = bitmap.PixelBuffer.AsStream())
        using (MemoryStream memoryStream = new MemoryStream())
        {
            stream.CopyTo(memoryStream);
            return memoryStream.ToArray();
        }
    }
Sign up to request clarification or add additional context in comments.

3 Comments

'IBuffer' does not contain a definition for 'AsStream' and the best extension method overload 'WindowsRuntimeStreamExtensions.AsStream(IRandomAccessStream)' requires a receiver of type 'IRandomAccessStream'
@V.G. Since it's an extension method, you'll need to add using System.Runtime.InteropServices.WindowsRuntime
This is the only answer (after extensive searching) that works in Windows Universal projects. .Net classes and namespaces have shifted since WPF, to Win 8 metro store to Universal Windows...so this answer is gold!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.