Results 1 to 3 of 3

Thread: Image -> Icon

  1. #1

    Thread Starter
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339

    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:
    1. 'has a casting problem
    2. sbpMsg.Icon = ilstMain.Images(2).Clone
    3.  
    4. 'same here
    5. sbpMsg.Icon = CType(ilstMain.Images(2).Clone, Icon)
    6.  
    7. 'says value can't be null
    8. Dim ms As MemoryStream = New MemoryStream()
    9. ilstMain.Images(2).Save(ms, ilstMain.Images(2).RawFormat)
    10. Dim ico As New Icon(ms)
    11. sbpMsg.Icon = ico
    12. ms.Close()
    13.  
    14. 'I've also tried switching the RawFormat to a format like
    15. ilstMain.Images(2).Save(ms, Drawing.Imaging.ImageFormat.Icon)
    16.  
    17. 'tried this too but that gets back to the casting problem
    18. ilstMain.Images(2).Save(ms, Drawing.Imaging.ImageFormat.Bmp)

    Thanks for any help you can offer.

  2. #2
    Member Vahid's Avatar
    Join Date
    Aug 2002
    Location
    Iran
    Posts
    37

    Wink

    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())

  3. #3

    Thread Starter
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    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
  •  



Click Here to Expand Forum to Full Width