title: c# UWP - Convert byte array to InMemoryRandomAccessStream/IRandomAccessStream
body: <p>I have a problem converting byte array to <code>InMemoryRandomAccessStream</code> or <code>IRandomAccessStream</code> in windows 8?</p>
  <p>This is my code, but It doesn't work :</p>
  <pre class="lang-cs prettyprint-override"><code>internal static async Task&lt;InMemoryRandomAccessStream&gt; ConvertTo(byte[] arr) 
  {
      InMemoryRandomAccessStream randomAccessStream = new InMemoryRandomAccessStream();
      
      Stream stream = randomAccessStream.AsStream();
      
      await stream.WriteAsync(arr, 0, arr.Length);
      await stream.FlushAsync();
  
      return randomAccessStream;
  }
  </code></pre>
  <p>And I created the <code>RandomAccessStreamReference</code> and set the requst datapack in order to share the image to other app</p>
  <pre class="lang-cs prettyprint-override"><code>private static async void OnDeferredImageStreamRequestedHandler(DataProviderRequest Request) 
  {
      DataProviderDeferral deferral = Request.GetDeferral();
      InMemoryRandomAccessStream stream = await ConvertTo(arr);
      
      RandomAccessStreamReference referenceStream =
              RandomAccessStreamReference.CreateFromStream(stream);
      
      Request.SetData(referenceStream);
  }
  </code></pre>
  <p>There's no exception but I can't use the result image byte array</p>
  <p>I think there's an error when converting <code>byte[]</code> to <code>InMemoryRandomAccessStream</code>, but it doesn't throw an exception.</p>
  <p>Anybody know how to implement it?</p>
  <p>if anyone knows how to convert the byte array to <code>IRandomAccessStream</code>, it's also appreciated</p>