15.4. The MemoryStream Class

A memory stream is a stream whose backing store is your computer's memory. Memory streams can reduce the need for temporary buffers and files in an application. Memory streams encapsulate data stored as an unsigned byte array.

The MemoryStream class is ideal for temporary storage of binary data. For example, to process an image file, you could read the file and save to a MemoryStream for later manipulation. The ToArray method of Memory Stream can then be later used to retrieve the byte[] representing the images even when the MemoryStream is closed. The following code snippet shows how to read an image file and store it in a MemoryStream:

 Image img = new Bitmap(@"C:\MyFace.gif"); MemoryStream output = new MemoryStream(); ...

Get .NET for Java Developers: Migrating to C# now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.