Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 624
DarioDP Avatar asked Apr 05 '14 09:04

DarioDP


People also ask

What is byte array?

A byte array is simply an area of memory containing a group of contiguous (side by side) bytes, such that it makes sense to talk about them in order: the first byte, the second byte etc..

How do you get a byte from a string?

There are two ways to convert byte array to String: By using String class constructor. By using UTF-8 encoding.


2 Answers

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

like image 193
Paul Annetts Avatar answered Oct 13 '22 11:10

Paul Annetts


I just don't want to create new question so decide to expand this one. It will be full answer for both platforms WP8 and Windows store app

Maybe you know how to convert IBuffer to byte[] in Windows Store App where we can't use this namespace: System.Runtime.InteropServices.WindowsRuntime

Regards, Marcin

Proposal solution: I use code from this post: https://stackoverflow.com/a/21490534/3139083

Thanks

like image 45
Marcin Avatar answered Oct 13 '22 10:10

Marcin