Convert MemoryStream to Byte Array

I need to convert a MemoryStream to a byte array..

Here is my code..

Dim ms As New MemoryStream(ccDocument) 'ccDocument is a byte array
Dim ccReturn As Byte(ms.Length)
ms.Read(ccReturn, 0, ms.Length)

Can anyone tell me if the above code is correct?... yes I know it comes in as a byte converts to stream and then back to byte....

Thanks
LVL 1
NTGuru705Asked:
Who is Participating?
 
Bob LearnedConnect With a Mentor Commented:
You could do something like this:

Dim ms As New MemoryStream(ccDocument) 'ccDocument is a byte array
Dim ccReturn As Byte() = ms.ToArray()

Bob
0
 
Bob LearnedCommented:
Here is an example class for image processing (using MemoryStream):

Imports System.IO
Imports System.Drawing.Imaging

Public Class ImageConverter

    Public Shared Function ByteArrayToImage(ByVal buffer() As Byte, ByVal fileName As String) As Image
        Using ms As New MemoryStream(buffer)
            Dim img As Image = Image.FromStream(ms)
            Return img
        End Using
    End Function

    Public Shared Function ImageToByteArray(ByVal img As Image, ByVal format As ImageFormat) As Byte()
        Using ms As New MemoryStream()
            img.Save(ms, format)
            Return ms.ToArray()
        End Using
    End Function

    Public Shared Function ImageToBase64String(ByVal img As Image, ByVal format As ImageFormat) As String
        Using ms As New MemoryStream()
            img.Save(ms, format)
            Return Convert.ToBase64String(ms.ToArray())
        End Using
    End Function

    Public Shared Function Base64StringToImage(ByVal imageString As String) As Image
        Dim buffer() As Byte = Convert.FromBase64String(imageString)
        Using ms As New MemoryStream(buffer)
            Return Image.FromStream(ms)
        End Using
    End Function

End Class

Bob
0
All Courses

From novice to tech pro — start learning today.