The Stream.CopyTo(memoryStream)
function copies bytes from the Stream
to the memoryStream
in C#. We can use the Stream.CopyTo()
function along with the object of the MemoryStream
class to convert a stream to a byte array. The following code example shows us how to convert a stream to a byte array with the Stream.CopyTo()
function in C#.
C# Code Example:
C# Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | class Program { public static byte[] Stream2ByteArray(Stream input) { MemoryStream ms = new MemoryStream(); input.CopyTo(ms); return ms.ToArray(); } static void Main(string[] args) { Stream stream = new MemoryStream(); Stream2ByteArray(stream); } } |