Skip to main content

Create a byte array from an input stream in C#.

// http://stackoverflow.com/questions/221925/creating-a-byte-array-from-a-stream
public static byte[] ToByteArray(this Stream input)
{
    using (var memoryStream = new MemoryStream())
    {
        input.CopyTo(memoryStream);
        return memoryStream.ToArray();
    }
}

// http://stackoverflow.com/questions/4736155/how-do-i-convert-byte-to-stream-in-c
public static Stream ToStream(this byte[] input)
{
    var fileStream = new MemoryStream(input) { Position = 0 };
    return fileStream;
}