Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I want to convert WriteableBitmap image to Byte array using C# code in Windows store metro style apps. Please give correct answer

share|improve this question

1 Answer

WriteableBitmap exposes PixelBuffer property of type IBuffer which is a Windows Runtime interface so it's a case of converting IBuffer to a byte array and this can be done through regular .NET Streams

byte[] ConvertBitmapToByteArray(WriteableBitmap bitmap)
{
    WriteableBitmap bmp = bitmap;

    using (Stream stream = bmp.PixelBuffer.AsStream())
    {
        MemoryStream memoryStream = new MemoryStream();
        stream.CopyTo(memoryStream);
        return memoryStream.ToArray();
    }
}

AsStream() is an extension method on IBuffer declared in WindowsRuntimeBufferExtensions class from System.Runtime.InteropServices.WindowsRuntime namespace.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.