c# UWP - Convert byte array to InMemoryRandomAccessStream/IRandomAccessStream
Asked Answered
P

3

19

I have a problem converting byte array to InMemoryRandomAccessStream or IRandomAccessStream in windows 8?

This is my code, but It doesn't work :

internal static async Task<InMemoryRandomAccessStream> ConvertTo(byte[] arr) 
{
    InMemoryRandomAccessStream randomAccessStream = new InMemoryRandomAccessStream();
    
    Stream stream = randomAccessStream.AsStream();
    
    await stream.WriteAsync(arr, 0, arr.Length);
    await stream.FlushAsync();

    return randomAccessStream;
}

And I created the RandomAccessStreamReference and set the requst datapack in order to share the image to other app

private static async void OnDeferredImageStreamRequestedHandler(DataProviderRequest Request) 
{
    DataProviderDeferral deferral = Request.GetDeferral();
    InMemoryRandomAccessStream stream = await ConvertTo(arr);
    
    RandomAccessStreamReference referenceStream =
            RandomAccessStreamReference.CreateFromStream(stream);
    
    Request.SetData(referenceStream);
}

There's no exception but I can't use the result image byte array

I think there's an error when converting byte[] to InMemoryRandomAccessStream, but it doesn't throw an exception.

Anybody know how to implement it?

if anyone knows how to convert the byte array to IRandomAccessStream, it's also appreciated

Pigeon answered 6/5, 2013 at 11:11 Comment(2)
What exactly is wrong with the code you have?Ratio
Please check this & tell me if you can solve the issue.Orvalorvan
P
25

Add the using statement at the top of the document.

using System.Runtime.InteropServices.WindowsRuntime;
internal static async Task<InMemoryRandomAccessStream> ConvertTo(byte[] arr)
{
    InMemoryRandomAccessStream randomAccessStream = new InMemoryRandomAccessStream();
    await randomAccessStream.WriteAsync(arr.AsBuffer());
    randomAccessStream.Seek(0); // Just to be sure.
                    // I don't think you need to flush here, but if it doesn't work, give it a try.
    return randomAccessStream;
}
Pleopod answered 9/5, 2013 at 17:21 Comment(0)
L
27

On Windows 8.1 it's even easier as we added the AsRandomAccessStream extension method:

internal static IRandomAccessStream ConvertTo(byte[] arr)
{
    MemoryStream stream = new MemoryStream(arr);
    return stream.AsRandomAccessStream();
}
Luz answered 28/6, 2013 at 0:22 Comment(1)
This seems not to work for some scenarios, as described in #22332717Crissycrist
P
25

Add the using statement at the top of the document.

using System.Runtime.InteropServices.WindowsRuntime;
internal static async Task<InMemoryRandomAccessStream> ConvertTo(byte[] arr)
{
    InMemoryRandomAccessStream randomAccessStream = new InMemoryRandomAccessStream();
    await randomAccessStream.WriteAsync(arr.AsBuffer());
    randomAccessStream.Seek(0); // Just to be sure.
                    // I don't think you need to flush here, but if it doesn't work, give it a try.
    return randomAccessStream;
}
Pleopod answered 9/5, 2013 at 17:21 Comment(0)
C
6

In one line:

internal static IRandomAccessStream ConvertTo(byte[] arr)
{
    return arr.AsBuffer().AsStream().AsRandomAccessStream();
}
Crissycrist answered 31/1, 2016 at 11:35 Comment(1)
It looks like an action that copies the data three times. You need to check that this is not happening in order for the answer to be usable.Wives

© 2022 - 2024 — McMap. All rights reserved.