2

So I have a Function that is supposed to convert an image to a byte() and then return a string. However, I am having some issues with this. Here is the function:

Shared Function saveSknI(ByVal tosave As Image) As String
    Dim converter As New ImageConverter
    Dim returnValue As String = ""
    For Each imageByte As Byte() In converter.ConvertTo(tosave, GetType(Byte()))
        returnValue = returnValue & Convert.ToBase64String(imageByte)
    Next
    Return returnValue
End Function

This Function returns an a System.InvalidCastException exception. It looks like this:

An unhandled exception of type 'System.InvalidCastException' occurred in ***.exe

Additional information: Unable to cast object of type 'System.Byte' to type 'System.Byte[]'.

I am using vb .net and I don't get why this isn't working, I've scanned the entire project for just byte not byte() and it didn't come up with anything.

2
  • 1
    Your function returns a String. Commented Jan 7, 2017 at 18:38
  • I made a mistake writing the first sentence, I have fixed it now. Commented Jan 7, 2017 at 19:14

1 Answer 1

2

Your method is declared as string:

Shared Function saveSknI(ByVal tosave As Image) As String

So it cant/wont return a byte array. Also, the image converter doesnt convert byte by byte. This line wont compile for me (using Option Strict):

For Each imageByte As Byte() In converter.ConvertTo(tosave, GetType(Byte()))

At a minimum, I think you meant For Each imageByte As Byte since there is only one array. Also, ConvertTo returns Object which you have not converted but are trying to iterate. You dont need to convert to Base64 byte by byte either. Corrected and collapsed:

Shared Function saveSknI(tosave As Image) As String
    Dim converter As New ImageConverter

    Dim bytes = DirectCast(converter.ConvertTo(tosave, GetType(Byte())), Byte())
    Return Convert.ToBase64String(bytes)
End Function

This does almost the same exact thing as ImageConverter without the boxing:

Public Function ToByteArray(img As Image, imgFormat As ImageFormat) As Byte()
    Dim tmpData As Byte()
    Using ms As New MemoryStream()
        img.Save(ms, imgFormat)
        tmpData = ms.ToArray
    End Using
    Return tmpData
End Function

Slap <Extension()> on it and place it in a module and you can use it as an extenstion:

imgBytes = myImg.ToByteArray(ImageFormat.PNG)

To convert the result to Base64:

imgB64 = Convert.ToBase64String(imgBytes)

If you so desired, you could create a ToBase64String() extension and do the conversion there in one step.

Sign up to request clarification or add additional context in comments.

2 Comments

How would you convert a byte into an image? Would it be similar?
Its basically the reverse. But, if you are starting with a B64 string, convert to byte() then create an image from a memstream created from the array. That can be written as an extension too: Dim img = myBytes.ToImage()

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.