Question

I have a code where i want to get an stream from an image and convert the memory stream to string array and store in a variable. But my problem is i also want to get the image from the string variable and paint on a picture box.

If i use this like PictureBox1.Image = image.FromStream(memoryStream) I am able to print the picture on picture box. But this is not my need. I just want to get the image stream from file and convert the stream as text and store it to some string variable and again i want to use the string variable and convert it to stream to print the image on picture box.

Here is my Code.(Vb Express 2008)

 Public Function ImageConversion(ByVal image As System.Drawing.Image) As String
        If image Is Nothing Then Return ""

        Dim memoryStream As System.IO.MemoryStream = New System.IO.MemoryStream
        image.Save(memoryStream, System.Drawing.Imaging.ImageFormat.Gif)

        Dim value As String = ""


        For intCnt As Integer = 0 To memoryStream.ToArray.Length - 1
            value = value & memoryStream.ToArray(intCnt) & "  "
        Next

        Dim strAsBytes() As Byte = New System.Text.UTF8Encoding().GetBytes(value)
        Dim ms As New System.IO.MemoryStream(strAsBytes)


        PictureBox1.Image = image.FromStream(ms)

        Return value
    End Function
Was it helpful?

Solution

This wouldn`t work in the way you have posted it (at least the part of recreating the image). See this:

Public Function ImageConversion(ByVal image As System.Drawing.Image) As String
    If image Is Nothing Then Return ""

    Dim memoryStream As System.IO.MemoryStream = New System.IO.MemoryStream
    image.Save(memoryStream, System.Drawing.Imaging.ImageFormat.Gif)

    Dim value As String = ""

    Dim content As Byte() = memoryStream.ToArray()
    ' instead of repeatingly call memoryStream.ToArray by using
    ' memoryStream.ToArray(intCnt)
    For intCnt As Integer = 0 To content.Length - 1
        value = value & content(intCnt) & "  "
    Next
    value = value.TrimEnd()

    Return value
End Function

To recreate the image using the created string you can`t use Encoding.GetBytes() like you did, because you would get a bytearray which represents your string. E.g "123 32 123" you would not get a byte array with the elements 123, 32 , 123

Public Function ImageConversion(ByVal stringRepOfImage As String) As System.Drawing.Image
    Dim stringBytes As String() = stringRepOfImage.Split(New String() {" "}, StringSplitOptions.RemoveEmptyEntries)
    Dim bytes As New List(Of Byte)
    For intCount = 0 To stringBytes.Length - 1
        Dim b As Byte
        If Byte.TryParse(stringBytes(intCount), b) Then
            bytes.Add(b)
        Else
            Throw new FormatException("Not a byte value")
        End If
    Next
    Dim ms As New System.IO.MemoryStream(bytes.ToArray)
    Return Image.FromStream(ms)
End Function

Reference: Byte.TryParse

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top