53

In Windows 8; I would like to pass the contents of a MemoryStream to a class that accepts a parameter of type Windows.Storage.Streams.IRandomAccessStream. Is there any way to convert this MemoryStream to an IRandomAccessStream?

1
  • Isn't there an extension method or something like .AsBuffer or the like? Oct 6, 2011 at 10:44

7 Answers 7

98

To use the extensions: you must add "using System.IO"

In Windows8, .NET and WinRT types are generally converted to/from compatible types under the hood so you don't have to care about it.

For streams, however, there are helper methods to convert between WinRT and .NET streams: For converting from WinRT streams -> .NET streams:

InMemoryRandomAccessStream win8Stream = GetData(); // Get a data stream from somewhere.
System.IO.Stream inputStream = win8Stream.AsStream()

For converting from .NET streams -> WinRT streams:

Windows.Storage.Streams.IInputStream inStream = stream.AsInputStream();
Windows.Storage.Streams.IOutputStream outStream = stream.AsOutputStream();

UPDATE: 2013-09-01

Let it not be said that Microsoft doesn't listen to it's developer community ;)

In the announcement for .NET FX 4.5.1, Microsoft states that:

Many of you have been wanting a way to convert a .NET Stream to a Windows Runtime IRandomAccessStream. Let’s just call it an AsRandomAccessStream extension method. We weren't able to get this feature into Windows 8, but it was one of our first additions to Windows 8.1 Preview.

You can now write the following code, to download an image with HttpClient, load it in a BitmapImage and then set as the source for a Xaml Image control.

    //access image via networking i/o
    var imageUrl = "http://www.microsoft.com/global/en-us/news/publishingimages/logos/MSFT_logo_Web.jpg";
    var client = new HttpClient();
    Stream stream = await client.GetStreamAsync(imageUrl);
    var memStream = new MemoryStream();
    await stream.CopyToAsync(memStream);
    memStream.Position = 0;
    var bitmap = new BitmapImage();
    bitmap.SetSource(memStream.AsRandomAccessStream());
    image.Source = bitmap;

HTH.

6
  • 7
    To use these extension methods you need to "using System.IO".
    – Whyllee
    Aug 4, 2012 at 8:08
  • 2
    This should be the answer. It is very straightforward. Use the extension method to convert a WinRT stream to .Net stream. Oct 28, 2012 at 5:25
  • um, the first code snipped does not compile but I get the point.
    – vidstige
    Apr 18, 2013 at 6:55
  • Thanks for the note. Fixed. Apr 22, 2013 at 21:36
  • Updated chosen answer -- this is more up-to-date and provides a more elegant way of doing it than was possible with the pre-release versions.
    – bbosak
    Jun 4, 2013 at 16:22
7

Found a more elegant solution:

public static class MicrosoftStreamExtensions
{
    public static IRandomAccessStream AsRandomAccessStream(this Stream stream)
    {
        return new RandomStream(stream);
    }

}

class RandomStream : IRandomAccessStream
{
    Stream internstream;

    public RandomStream(Stream underlyingstream)
    {
        internstream = underlyingstream;
    }

    public IInputStream GetInputStreamAt(ulong position)
    {
        //THANKS Microsoft! This is GREATLY appreciated!
        internstream.Position = (long)position;
        return internstream.AsInputStream();
    }

    public IOutputStream GetOutputStreamAt(ulong position)
    {
        internstream.Position = (long)position;
        return internstream.AsOutputStream();
    }

    public ulong Size
    {
        get
        {
            return (ulong)internstream.Length;
        }
        set
        {
            internstream.SetLength((long)value);
        }
    }

    public bool CanRead
    {
        get { return this.internstream.CanRead; }
    }

    public bool CanWrite
    {
        get { return this.internstream.CanWrite; }
    }

    public IRandomAccessStream CloneStream()
    {
        throw new NotSupportedException();
    }

    public ulong Position
    {
        get { return (ulong)this.internstream.Position; }
    }

    public void Seek(ulong position)
    {
        this.internstream.Seek((long)position, SeekOrigin.Begin);
    }

    public void Dispose()
    {
        this.internstream.Dispose();
    }

    public Windows.Foundation.IAsyncOperationWithProgress ReadAsync(IBuffer buffer, uint count, InputStreamOptions options)
    {
        return this.GetInputStreamAt(this.Position).ReadAsync(buffer, count, options);
    }

    public Windows.Foundation.IAsyncOperation FlushAsync()
    {
        return this.GetOutputStreamAt(this.Position).FlushAsync();
    }

    public Windows.Foundation.IAsyncOperationWithProgress WriteAsync(IBuffer buffer)
    {
        return this.GetOutputStreamAt(this.Position).WriteAsync(buffer);
    }
}
7
  • Very nice. I was looking for such wrapper from Microsoft and didn't succeed. Oct 6, 2011 at 16:32
  • 2
    This solution has two problems, though. 1) If somebody calls to GetInputStreamAt(0), then GetOutputStreamAt(1), position of input stream will be also changed to 1. 2) It accepts any stream, so CanSeek property may be equal to false. The NotSupportedException will be thrown in this case. Copying stream is less efficient for big steams but should not have these problems. Oct 6, 2011 at 17:24
  • @RomanBoiko Granted, it's not a general solution for any type of stream, but it serves its purpose for many types of applications which would still use the new type of stream as though it were the old method (for example, just reading data from the stream and passing it into a WinRT component, then getting rid of the IRandomAccessStream. I should also point out that IRandomAccessStream was not intended to be used as a System.IO.Stream, and vice-versa, but this serves the purpose for when you want to use an IRandomAccessStream as though it were a Stream.
    – bbosak
    Oct 6, 2011 at 22:14
  • It's a nice and simple solution for many problems when you know exactly that nobody is going to call both GetInputStreamAt(someNumber) and GetOutputStreamAt(otherNumber) and use them both in the same time period. It can be extended to prevent such situations by storing a flag that Position has been changed and throwing if it is changed later. Some bugs may still appear, though... The problems which I highlighted deal with breaking contract of the IRandomAccessStream, although it has not been documented yet. Oct 7, 2011 at 7:06
  • @RomanBoiko Microsoft seems to have very poor quality documentation for Windows 8, very few code samples, and lots of TBDs (to-be-determined's)
    – bbosak
    Oct 7, 2011 at 20:23
5

After some experimenting I found the following code to be working.

using System;
using System.IO;
using System.Threading.Tasks;
using Windows.Storage.Streams;

partial class MainPage
{
    public MainPage()
    {
        var memoryStream = new MemoryStream(new byte[] { 65, 66, 67 });
        ConvertToRandomAccessStream(memoryStream, UseRandomAccessStream);
        InitializeComponent();
    }

    void UseRandomAccessStream(IRandomAccessStream stream)
    {
        var size = stream.Size;
    } // put breakpoint here to check size

    private static async void ConvertToRandomAccessStream(MemoryStream memoryStream,
         Action<IRandomAccessStream> callback)
    {
        var randomAccessStream = new InMemoryRandomAccessStream();
        var outputStream = randomAccessStream.GetOutputStreamAt(0);
        var dw = new DataWriter(outputStream);
        var task = new Task(() => dw.WriteBytes(memoryStream.ToArray()));
        task.Start();
        await task;
        await dw.StoreAsync();
        var success = await outputStream.FlushAsync();
        callback(randomAccessStream);
    }
}

UPDATE: I also tried more elegant method implementation:

    private static void ConvertToRandomAccessStream(MemoryStream memoryStream,
        Action<IRandomAccessStream> callback)
    {
        var randomAccessStream = new InMemoryRandomAccessStream();
        var outputStream = randomAccessStream.GetOutputStreamAt(0);
        RandomAccessStream.Copy(memoryStream.AsInputStream(), outputStream);
        callback(randomAccessStream);
    }

Strangely, it doesn't work. When I call stream.Size later, I get zero.

UPDATE I changed the function to return the IRandomAccessStream rather than using the callback function

public static async Task<IRandomAccessStream> ConvertToRandomAccessStream(MemoryStream memoryStream)
{
    var randomAccessStream = new InMemoryRandomAccessStream();

    var outputStream = randomAccessStream.GetOutputStreamAt(0);
    var dw = new DataWriter(outputStream);
    var task = new Task(() => dw.WriteBytes(memoryStream.ToArray()));
    task.Start();

    await task;
    await dw.StoreAsync();

    await outputStream.FlushAsync();

    return randomAccessStream;
}
0
4

There is no built-in way method on Windows 8. For Windows 8.1 we added a Stream.AsRandomAccessStream() extension method:

internal static IRandomAccessStream ToRandomAccessStream(byte[] array)
{
    MemoryStream stream = new MemoryStream(array);
    return stream.AsRandomAccessStream();
}
3

None of the above works for me today (maybe some API changes since the answers were posted). The only way that works is

IRandomAccessStream inMemoryStream = new InMemoryRandomAccessStream();
using (var inputStream = stream.AsInputStream())
{
    await RandomAccessStream.CopyAsync(inputStream, inMemoryStream);
}
inMemoryStream.Seek(0);
0

This code snippet turns a stream (stream) into an InMemoryRandomAccessStream (ims) which implement IRandomAccessStream. The trick is that CopyTo has to be called on a background thread.

        InMemoryRandomAccessStream ims = new InMemoryRandomAccessStream();
        var imsWriter = ims.OpenWrite();
        await Task.Factory.StartNew(() => stream.CopyTo(imsWriter));
3
  • 1
    Instead of the task bit on the last line with: await stream.CopyToAsync(imsWriter); Sep 15, 2012 at 11:00
  • InMemoryRandomAccessStream.OpenWrite() doesn't seem to exist anymore? Jan 13, 2013 at 12:26
  • 1
    @JayBorseth why? According to MSDN, it does exist. Jan 27, 2013 at 23:53
0

Take a look to this link:

How To Convert Byte Array To IRandomAccessStream

It also gives examples and an implementation of a byte array constructor (and one for .NET streams), useful if you want to use the SetSource or SetSourceAsync methods of BitmapImage class (as in my case).

Hope this helps someone...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.