How can I convert Windows.Storage.Streams.IRandomAccessStream
to System.IO.Stream
?
I'm using a C# library which accepts System.IO.Stream
as input, but when I open files in Metro I get Windows.Storage.Streams.IRandomAccessStream
.
The easiest way is to call AsStream
.
You can convert Windows.Storage.Streams.IRandomAccessStream
to a byte[]
and then convert byte[]
to a System.IO.Stream
.
Byte[] from 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);
Stream from Byte[]
var stream = new MemoryStream(bytes);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With