If you want to read from the memory stream, you need to make sure that the current position of the stream is at the beginning.
Also, you are using the Read
method wrong. It returns the number of bytes read, which may be less than the number of bytes requested. To use it correctly you would need to loop until you have got all the bytes in the stream.
However, you should just use the ToArray
method to get everything in the stream as a byte array:
Private Function GetStreamAsByteArray(ByVal stream As MemoryStream) As Byte()
Return stream.ToArray()
End Function