944

How do I convert struct System.Byte byte[] to a System.IO.Stream object in C#?

Arsen Khachaturyan
  • 7,904
  • 4
  • 42
  • 42
Mehdi Hadeli
  • 9,784
  • 3
  • 20
  • 20

6 Answers6

1599

The easiest way to convert a byte array to a stream is using the MemoryStream class:

Stream stream = new MemoryStream(byteArray);
Martin Buberl
  • 45,844
  • 25
  • 100
  • 144
  • 21
    Please note that this way of creating a stream is perhaps not ideal: "This constructor does not expose the underlying stream. GetBuffer throws UnauthorizedAccessException." http://msdn.microsoft.com/en-us/library/e55f3s5k.aspx – noocyte Apr 01 '13 at 16:03
  • 33
    @noocyte what is the ideal way then? – brettwhiteman Nov 14 '15 at 00:08
  • 9
    but you can still use `stream.ToArray()` if you want your byte array back. – Florian F Mar 26 '16 at 22:38
  • 3
    What kind of overhead is associated with constructing a stream from the byte array like this? Memory usage is mostly what I'm wondering about. – jocull Sep 14 '17 at 14:52
  • 2
    streams created with this method can not be further expend. – Muhammad Saqib Jul 10 '20 at 22:22
423

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);
Arsen Khachaturyan
  • 7,904
  • 4
  • 42
  • 42
Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
  • 34
    This 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. – Jduv Jul 19 '12 at 03:56
  • 24
    Also remember to reset stream afterward: stream.Seek(0, SeekOrigin.Begin); – Minh Nguyen Oct 14 '15 at 14:08
  • 3
    Please note that the first option `MemoryStream.Write` is much more memory consuming then `new MemoryStream(myByteArray)` – Alex from Jitbit Jul 14 '17 at 20:21
  • 1
    Why, 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 - on strike Jul 16 '17 at 11:45
  • 5
    There'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 the `byte[]` constructor, the array isn't copied - the `MemoryStream` 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 :) – Luaan Aug 02 '17 at 08:10
35

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);
    }
}
johnnyRose
  • 7,310
  • 17
  • 40
  • 61
QrystaL
  • 4,886
  • 2
  • 24
  • 28
8

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
    };
}
Rod Talingting
  • 943
  • 1
  • 15
  • 25
5

Look into the MemoryStream class.

Vadim Ovchinnikov
  • 13,327
  • 5
  • 62
  • 90
Corey Ogburn
  • 24,072
  • 31
  • 113
  • 188
0
Stream into Byte[]:                                   

MemoryStream memory = (MemoryStream)stream; 

byte[] imageData = memory.ToArray();
Fogh
  • 1,275
  • 1
  • 21
  • 29