How do I convert struct System.Byte
byte[]
to a System.IO.Stream
object in C#?
6 Answers
The easiest way to convert a byte array to a stream is using the MemoryStream
class:
Stream stream = new MemoryStream(byteArray);
-
22Please note that this way of creating a stream is perhaps not ideal: "This constructor does not expose the underlying stream. GetBuffer throws UnauthorizedAccessException." msdn.microsoft.com/en-us/library/e55f3s5k.aspx– noocyteCommented Apr 1, 2013 at 16:03
-
35
-
10but you can still use
stream.ToArray()
if you want your byte array back. Commented Mar 26, 2016 at 22:38 -
3What kind of overhead is associated with constructing a stream from the byte array like this? Memory usage is mostly what I'm wondering about.– jocullCommented Sep 14, 2017 at 14:52
-
2
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);
-
34This is the best answer. It's concise and covers all the practical applications. There's a gotcha with just using the byte array based constructor as indicated here--the resulting stream is not re-sizable.– JduvCommented Jul 19, 2012 at 3:56
-
25Also remember to reset stream afterward: stream.Seek(0, SeekOrigin.Begin); Commented Oct 14, 2015 at 14:08
-
4Please note that the first option
MemoryStream.Write
is much more memory consuming thennew MemoryStream(myByteArray)
Commented Jul 14, 2017 at 20:21 -
1Why, exactly, is that @jitbit? It's been many years since I did any .NET, so if I were going to update this answer to comment on performance, I'd need some more information.– Cody Gray ♦Commented Jul 16, 2017 at 11:45
-
5There's extra space allocated in the
MemoryStream
buffer by default (just like with e.g. a list). This can be dealt with easily by using the overload that allows you to set capacity, but is only really useful if you don't expect to write any data to the stream (or if you know how much extra bytes you're likely to need). But I suspect that jitbit might be referring to the fact that a when you use thebyte[]
constructor, the array isn't copied - theMemoryStream
refers to the array in the argument. This can be both good and bad, and it's a bit of a shame it isn't documented on MSDN :)– LuaanCommented Aug 2, 2017 at 8:10
The general approach to write to any stream (not only MemoryStream
) is to use BinaryWriter
:
static void Write(Stream s, Byte[] bytes)
{
using (var writer = new BinaryWriter(s))
{
writer.Write(bytes);
}
}
If you are getting an error with the other MemoryStream examples here, then you need to set the Position to 0.
public static Stream ToStream(this bytes[] bytes)
{
return new MemoryStream(bytes)
{
Position = 0
};
}
-
Why may one need to set position to zero, do you know ? Commented Jun 6, 2022 at 10:46
-
1@TheLegendaryCopyCoder, chances are you want to read the stream from the beginning after getting it. Position 0 is the beginning. Commented Sep 1, 2022 at 8:27
Stream into Byte[]:
MemoryStream memory = (MemoryStream)stream;
byte[] imageData = memory.ToArray();