|
-
Aug 27th, 2002, 12:28 PM
#1
Image -> Icon
How can you convert an Image object to be used where an Icon object is needed, without writing it to a file first?
I have an object that already has an Image and I want to use that Image in a statusbar panel or an image from my imagelist but I get a 'Specified cast is not valid'. Even when the item in the imagelist was an icon to begin with. I also tried the IconConverter class with no luck. I also tried saving the Image to a MemoryStream and then geting the Icon from the stream but It seems the RawFormat of an Icon is null so it complains about that.
Am I stuck writing it to a file and then loading it as the icon and deleting the file?
Here is some of the code I've tried:
VB Code:
'has a casting problem
sbpMsg.Icon = ilstMain.Images(2).Clone
'same here
sbpMsg.Icon = CType(ilstMain.Images(2).Clone, Icon)
'says value can't be null
Dim ms As MemoryStream = New MemoryStream()
ilstMain.Images(2).Save(ms, ilstMain.Images(2).RawFormat)
Dim ico As New Icon(ms)
sbpMsg.Icon = ico
ms.Close()
'I've also tried switching the RawFormat to a format like
ilstMain.Images(2).Save(ms, Drawing.Imaging.ImageFormat.Icon)
'tried this too but that gets back to the casting problem
ilstMain.Images(2).Save(ms, Drawing.Imaging.ImageFormat.Bmp)
Thanks for any help you can offer.
-
Aug 28th, 2002, 04:58 PM
#2
Member
Hey pal, To convert Icon to Image:
Dim myIcon As System.Drawing.Icon
Dim myImage As System.Drawing.Image
myImage = CType(myIcon.ToBitmap, System.Drawing.Image)
And to convert Image to Icon:
myIcon = System.Drawing.Icon.FromHandle(CType(myImage, System.Drawing.Bitmap).GetHicon())
-
Aug 28th, 2002, 05:12 PM
#3
You the man, Vahid!
Thanks a lot that one was troubling me.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|