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

2

17

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?

Blunt answered 5/4, 2014 at 9:50 Comment(1)
Can you show some code? In particular how do you create the IBuffer - what's the actual object that implements the interface?Immobilize
I
23

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

Immobilize answered 5/4, 2014 at 13:57 Comment(1)
ToArray() doesn't always return the data correctly. Sometimes, you need to get at it with reflection. This can be because the implementation of the interface might have bugsBrisesoleil
W
3

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://mcmap.net/q/744824/-readtextasync-in-windows-store-app-with-multibyte-file-and-javascript

Thanks

Woodley answered 29/8, 2014 at 8:29 Comment(1)
Just a note for future reference, the guidelines for StackOverflow indicate that you should start a new question rather than expanding on one. Its generally accepted to start a new question if this one doesn't answer your use case specifically.Same

© 2022 - 2024 — McMap. All rights reserved.