using System.IO; // create a MemoryStream var memoryStream = new MemoryStream(); // write a string to the memory stream var myString = "Hello, world!"; var buffer = Encoding.ASCII.GetBytes(myString); memoryStream.Write(buffer, 0, buffer.Length); // convert the memory stream to an output stream var outputStream = memoryStream.AsOutputStream();
using System.IO; using System.Drawing; // create a MemoryStream var memoryStream = new MemoryStream(); // load an image from a file var image = Image.FromFile("myImage.jpg"); // save the image to the memory stream as a PNG image.Save(memoryStream, ImageFormat.Png); // convert the memory stream to an output stream var outputStream = memoryStream.AsOutputStream();Both examples use the System.IO namespace and MemoryStream class.