How do I convert struct System.Byte
byte[]
to a System.IO.Stream
object in C#?
831
votes
5 Answers
1427
votes
The easiest way to convert a byte array to a stream is using the MemoryStream
class:
Stream stream = new MemoryStream(byteArray);
370
votes
You're looking for the MemoryStream.Write
method.
For example, the following code will write the contents of a byte[]
array into a memory stream:
byte[] myByteArray = new byte[10];
MemoryStream stream = new MemoryStream();
stream.Write(myByteArray, 0, myByteArray.Length);
Alternatively, you could create a new, non-resizable MemoryStream
object based on the byte array:
byte[] myByteArray = new byte[10];
MemoryStream stream = new MemoryStream(myByteArray);
31
votes
4
votes