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.




Reply With Quote