Convert an IBuffer to a byte array in Windows Phone 8.1, how?

AshleyT picture AshleyT · Apr 5, 2014 · Viewed 12.4k times · Source

I'm writing an application for Windows Phone 8.1.

I need to save an UIElement as an image file (I'd prefer JPG or PNG). I'm using RenderTargetBitmap class to do this. After calling the method RenderAsync on my UIElement I create an IBuffer that contains the result of the method GetPixelsAsync() of my RenderTargetBitmap.

Now I need to call the method ToArray() to convert my IBuffer in a byte array to save my image using something like BitmapEncoder, but it seems that on Windows Phone 8.1 there isn't any ToArray() method for IBuffer, while on Windows 8.1 is present.

How can I solve this issue?

Answer

Paul Annetts picture Paul Annetts · Apr 5, 2014

This is available on Windows Phone 8.1, but it not available on WP 8.0. (see MSDN link)

You need to make sure that you include the appropriate namespace however so it is found by the compiler:

using System.Runtime.InteropServices.WindowsRuntime