Specified Cast not Valid: Object to Bitmap

J

JL

My project has a series of icons; 20x20 pictureboxes populated with
bitmaps. The bitmaps/images come from a DLL. The VB.NET DLL that is
called has a form in it, which has pictureboxes containing each
available picture. A picture is requested from the DLL as follows:

Public Function ReturnIcon() As Object
Return New frmImages().picLiteOn.Image
End Function

Because of the technique used to call the DLL, the return must be an
object. (that's a whole other story) The code in the program requests
the picture from the DLL as follows:

Dim OBJ As Object = New DLLName.Class1().ReturnIcon
Dim BMP As Bitmap
Try
BMP = CType(OBJ, Bitmap)
Catch ex As Exception
MsgBox(ex.ToString)
End Try
CType(CTRL, PictureBox).Image = BMP

The program "crashes" on BMP=CType(OBJ,Bitmap) with this error:
System.InvalidCastException: Specified cast is not valid.

The interesting thing is, if I step through the code, after the error,
I can go to the Command Window and type in BMP=CType(OBJ,Bitmap), it
is accepted and the picture box loads with no errors.

Any suggestions?

Thx. JL.
 
J

JL

Just wanted the group to know that I found a solution to this problem.
The problem was in converting an image to an object and then back to
an image. Instead, I converted it from an image to a byte array then
to an object, then the reverse, object to byte array to image. I don't
know if that is the "ideal" or proper solution, but it works. JL

'<<Image to Object>>
public function GetPicture() as Object
Dim IMG As System.Drawing.Image
IMG = New frmImages().picBox.Image
'Create a Memory Stream and put the Picture in it.
Dim MS As New IO.MemoryStream
IMG.Save(MS, System.Drawing.Imaging.ImageFormat.Bmp)
'Create a Byte Array, put the Memory Stream in it.
Dim B() As Byte
B = MS.ToArray
'Return the Byte Array as an Object
Return B
End Function

'<<Object back to Image>>
dim OBJ as object = GetPicture
'Convert the Object to a Byte Array
Dim B() As Byte = CType(OBJ, Byte())
'Convert the Byte Array to a Memory Stream
Dim STRM As New System.IO.MemoryStream(B)
'Put the memory stream into the picture box.
CType(CTRL, PictureBox).Image = New Bitmap(STRM)
 
G

Guest

Hi, this is about the closest thing I could find to what I was looking for,
cheers JL, I haven't manage to get my code working though, iu have tried
several different variations they all end up giving me an image that is just
pure black,
anybody any ideas, I'm prettu sure the object stuff is not needed but it
didn't make any difference either way anyway here is my code
Dim bm As Bitmap
Dim bm2 As Bitmap
Dim bitmapbytestream() As Byte
Dim b2() As Byte
Dim ms As New MemoryStream()
Dim ourpic As Object
'


bm.Save(ms, System.Drawing.Imaging.ImageFormat.Bmp)
bitmapbytestream = ms.ToArray
ourpic = bitmapbytestream
b2 = CType(ourpic, Byte())
Dim STRM As New MemoryStream(b2)
CType(pboCard, PictureBox).Image = New Bitmap(STRM)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top