Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / New Users / August 2004

Tip: Looking for answers? Try searching our database.

Specified Cast not Valid: Object to Bitmap

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
JL - 19 Jun 2004 16:43 GMT
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.
JL - 22 Jun 2004 15:40 GMT
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)

> 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
[quoted text clipped - 28 lines]
>
> Thx. JL.
vbdotnetmania - 24 Aug 2004 10:59 GMT
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)

> 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
[quoted text clipped - 57 lines]
> >
> > Thx. JL.

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.