How to convert an Stream into a byte[] in C#?

pupeno picture pupeno · Jul 3, 2009 · Viewed 490.4k times · Source

Is there a simple way or method to convert an Stream into a byte[] in C#?

Answer

James Dingle picture James Dingle · Aug 16, 2011

The shortest solution I know:

using(var memoryStream = new MemoryStream())
{
  sourceStream.CopyTo(memoryStream);
  return memoryStream.ToArray();
}