Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert a byte array to Stream [duplicate]

Tags:

c#

Possible Duplicate:
How do I convert byte[] to stream in C#?

I need to convert a byte array to a Stream . How to do so in C#?

It is in asp.net application.

FileUpload Control Name: taxformUpload

Program

byte[] buffer = new byte[(int)taxformUpload.FileContent.Length]; taxformUpload.FileContent.Read(buffer, 0, buffer.Length);  Stream stream = ConvertToStream(buffer); 
like image 759
priyanka.sarkar Avatar asked Mar 29 '12 03:03

priyanka.sarkar


People also ask

Is a byte array a stream?

Streams are commonly written to byte arrays. The byte array is the preferred way to reference binary data in . NET. It can be used to data like the contents of a file or the pixels that make up the bitmap for an image.

Does MemoryStream copy byte array?

From expirience I can say, that it does not copy the array. Be aware though, that you are unable to resize the memory stream, when using an array in the constructor.

How do I reuse MemoryStream?

You can re-use the MemoryStream by Setting the Position to 0 and the Length to 0. By setting the length to 0 you do not clear the existing buffer, it only resets the internal counters.


Video Answer


1 Answers

Easy, simply wrap a MemoryStream around it:

Stream stream = new MemoryStream(buffer); 
like image 187
Etienne de Martel Avatar answered Oct 02 '22 14:10

Etienne de Martel