I created a simple extension to convert a Stream to Byte array:
public static Byte[] ToByte(this Stream value)
{
if (value == null)
return null;
if (value is MemoryStream)
{
return ((MemoryStream)value).ToArray();
}
else
{
using (MemoryStream stream = new MemoryStream())
{
value.CopyTo(stream);
return stream.ToArray();
}
}
}
This works fine the first time I use it ...
If I use it a second time on the same stream the resulting Byte[] is Byte[0].
I debugged and I think this happens because after the first conversion the Stream index goes to the end of the stream.
What is the correct way to fix this?