You write a stream, and then immediately try to read it without rewinding ... so there is no data to read.
Fortunately, there is a very simple way to simplify the code:
byte[] file = stream.ToArray();
However, you have another potential problem:
byte[] storage = new byte[500000]; using (MemoryStream stream = new MemoryStream(storage)) ...
This will lead to the fact that the MemoryStream will have a fixed size of 500K - no more, no less. I suspect that is not what you want; I suggest you get rid of the storage variable and just call the constructor without MemoryStream parameters.
Jon skeet
source share