Doug Null
Doug Null

Reputation: 8327

'ImageConverter' can't convert a Bitmap to a byte array. gets error

Error is: "'ImageConverter' is unable to convert 'System.Drawing.Bitmap' to 'System.Byte'."

dim YZ_2D_blobmap( 150 * 100 * 3 ) as byte 
dim heatmap_PictureBox_Bitmap   as Bitmap

' . . .heatmap_PictureBox_Bitmap loaded with 150 x 100 bitmap

   YZ_2D_blobmap = Bitmap_to_Bytes( heatmap_PictureBox_Bitmap ) <<<<<<< error
    bytes_to_file( YZ_2D_blobmap, YZ_2D_BLOBMAP_BLB_PATHNAME )

        Function Bitmap_to_Bytes(  img as Bitmap ) as byte()
            dim bytes_ImageConverter as ImageConverter = New ImageConverter()
            return bytes_ImageConverter.ConvertTo( img, GetType( byte ))
         end function

         Sub bytes_to_file( byte_buffer, pathname )
            system.io.file.writeAllBytes( pathname, byte_buffer  )
         End Sub

Upvotes: 0

Views: 770

Answers (1)

jmcilhinney
jmcilhinney

Reputation: 54427

You are telling the ImageConverter to convert the Bitmap to a Byte rather than to a Byte array. This:

return bytes_ImageConverter.ConvertTo( img, GetType( byte ))

should be this:

Return bytes_ImageConverter.ConvertTo(img, GetType(Byte()))

The error message even tells you that:

'ImageConverter' is unable to convert 'System.Drawing.Bitmap' to 'System.Byte'.

Nothing there about arrays.

Upvotes: 2

Related Questions