Monday, March 16, 2009

C#: Convert Non-Seekable Stream to Byte Array

byte[] ConvertNonSeekableStreamToByteArray(Stream NonSeekableStream)
{
MemoryStream ms = new MemoryStream();
byte[] buffer = new byte[1024];
int bytes;
while ((bytes = NonSeekableStream.Read(buffer, 0, buffer.Length)) > 0)
{
ms.Write(buffer, 0, bytes);
}
byte[] output = ms.ToArray();
return output;
}