将Windows.Storage.Streams.IRandomAccessStream转换为System.IO.Stream

时间:2012-11-29 21:58:52

标签: windows-8 microsoft-metro

如何将Windows.Storage.Streams.IRandomAccessStream转换为System.IO.Stream

我正在使用一个接受System.IO.Stream作为输入的C#库,但是当我在Metro中打开文件时,我得到Windows.Storage.Streams.IRandomAccessStream

2 个答案:

答案 0 :(得分:12)

最简单的方法是致电AsStream

答案 1 :(得分:7)

您可以将Windows.Storage.Streams.IRandomAccessStream转换为byte[],然后将byte[]转换为System.IO.Stream

来自IRandomAccessStream的字节[]

        var file = await new FileOpenPicker().PickSingleFileAsync();
        var fStream = await file.OpenAsync(FileAccessMode.Read);

        var reader = new DataReader(fStream.GetInputStreamAt(0));
        var bytes = new byte[fStream.Size];
        await reader.LoadAsync((uint)fStream.Size);
        reader.ReadBytes(bytes);

来自字节流[]

var stream = new MemoryStream(bytes);