byte型の配列からIRandomAccessStream型のストリームへ変換する
byte型配列からWindows.Storage.Streams.IRandomAccessStream型のストリームへの変換です。
Windowsストアアプリでは、System.IO.WindowsRuntimeStreamExtensionsクラスが提供されています。このWindowsRuntimeStreamExtensionsクラスを使って、ストリーム関係の変換を非常に簡単におこなうことができます。
System.IO.Stream型のストリームからWindows.Storage.Streams.IRandomAccessStream型のストリームへ変換するには、AsRandomAccessStreamメソッドを使います。
// using System.IO; // using Windows.Storage.Streams; // using System.Runtime.InteropServices.WindowsRuntime; // byte型配列をIBufferへ変換する var buffer = bytes.AsBuffer(); var stream = new Windows.Storage.Streams.InMemoryRandomAccessStream(); await stream.WriteAsync(buffer); return stream;
IRandomAccessStream型のストリームからbyte型の配列へ変換する
Windows.Storage.Streams.IRandomAccessStream型のストリームからbyte型配列への変換です。
IRandomAccessStreamクラスのReadBytesAsyncメソッドを使ってbyte型配列を読み込みます。
// using System.IO; // using Windows.Storage.Streams; IRandomAccessStream stream = GetXXXXX(); var bytes = await stream.ReadBytesAsync();