Windows apps
Collapse the table of content
Expand the table of content

WindowsRuntimeBufferExtensions.GetWindowsRuntimeBuffer Method (MemoryStream, Int32, Int32)

.NET Framework (current version)

Returns a Windows.Storage.Streams.IBuffer interface that represents a region within the memory that the specified memory stream represents.

This API is not CLS-compliant. 

Namespace:  System.Runtime.InteropServices.WindowsRuntime
Assembly:  System.Runtime.WindowsRuntime (in System.Runtime.WindowsRuntime.dll)

public static IBuffer GetWindowsRuntimeBuffer(
	this MemoryStream underlyingStream,
	int positionInStream,
	int length


Type: System.IO.MemoryStream

The stream that shares memory with the IBuffer.

Type: System.Int32

The position of the shared memory region in underlyingStream.

Type: System.Int32

The maximum size of the shared memory region. If the number of bytes in underlyingStream, starting at positionInStream, is less than length, the IBuffer that is returned represents only the available bytes.

Return Value

Type: IBuffer
A Windows.Storage.Streams.IBuffer interface that is backed by a region within the memory that backs the specified memory stream.

Usage Note

In Visual Basic and C#, you can call this method as an instance method on any object of type MemoryStream. When you use instance method syntax to call this method, omit the first parameter. For more information, see Extension Methods (Visual Basic) or Extension Methods (C# Programming Guide).


underlyingStream is null.


positionInStream or length is less than 0 (zero).


positionInStream is beyond the end of source.


underlyingStream cannot expose its underlying memory buffer.


underlyingStream has been closed.

The Capacity property of the IBuffer is set to length or to the number of bytes between positionInStream and the capacity of the stream, whichever is less. The Length property of the IBuffer is set to 0 (zero) if positionInStream is beyond length of the stream; otherwise, it is set to length or to the number of bytes between positionInStream and the length of the stream, whichever is less.

If underlyingStream is resized, it may represent a different region of memory; however, the IBuffer continues to represent the memory of the original stream before it was resized.

.NET Framework

Supported in: 4.6, 4.5

.NET for Windows Phone apps

Supported in: Windows Phone Silverlight 8
© 2018 Microsoft