如何从IRandomAccessStream获取IBuffer

时间:2015-03-09 13:43:32

标签: c# windows-8.1 win-universal-app

如果我有一个IRandomAccessStream对象,我该如何创建一个IBuffer对象?

1 个答案:

答案 0 :(得分:6)

我找到的最简单方法是通过MemoryStream.GetWindowsRuntimeBuffer:

IRandomAccessStream stream = ...;

using (var memoryStream = new MemoryStream())
{
    memoryStream.Capacity = (int)stream.Size;
    var ibuffer = memoryStream.GetWindowsRuntimeBuffer();
    await stream.ReadAsync(ibuffer, (uint)stream.Size, InputStreamOptions.None).AsTask().ConfigureAwait(false);
}