To convert Stream to ByteArray in C# and VB.NET you can use the following snippet.
Samples
Sample C#
public static byte[] StreamToByteArray(Stream inputStream) { byte[] bytes = new byte[16384]; using (MemoryStream memoryStream = new MemoryStream()) { int count; while ((count = inputStream.Read(bytes, 0, bytes.Length)) > 0) { memoryStream.Write(bytes, 0, count); } return memoryStream.ToArray(); } }
Sample VB.NET
Public Function StreamToByteArray(inputStream As Stream) As Byte() Dim bytes = New Byte(16383) {} Using memoryStream = New MemoryStream() Dim count As Integer While ((count = inputStream.Read(bytes, 0, bytes.Length)) > 0) memoryStream.Write(bytes, 0, count) End While Return memoryStream.ToArray() End Using End Function
If you have any questions or suggestions feel free to rate this snippet, post a comment or Contact Us via Email.
Related links:
- Stream Class
- Byte Array
- MemoryStream Class
- Stream.Read Method (Byte[], Int32, Int32)
- MemoryStream.ToArray Method ()
- MemoryStream.Write Method (Byte[], Int32, Int32)
RT @CodeSnippetsNET: How to convert Stream to ByteArray in C# and #VB .NET http://t.co/9gqeOEQWwi #csharp #dotnet #visualbasic #programming…
RT @CodeSnippetsNET: How to convert Stream to ByteArray in C# and #VB .NET http://t.co/9gqeOEQWwi #csharp #dotnet #visualbasic #programming…
Convert Stream to ByteArray in #Csharp and #VB #DOTNET -> https://t.co/CypcbZnZPD <- #programming #coding #developers
RT @CodeSnippetsNET: Convert Stream to ByteArray in #Csharp and #VB #DOTNET -> https://t.co/CypcbZnZPD <- #programming #coding #developers
RT @CodeSnippetsNET: Convert Stream to ByteArray in #Csharp and #VB #DOTNET -> https://t.co/CypcbZnZPD <- #programming #coding #developers
RT @CodeSnippetsNET: Convert Stream to ByteArray in #Csharp and #VB #DOTNET -> https://t.co/CypcbZnZPD <- #programming #coding #developers