RSS 2.0 Feed
RSS 2.0


Atom 1.0 Feed
Atom 1.0

  Using images in an ImageList as Icons 


At times you might want to take images from an ImageList and use them as Icons in your application with things like a NotifyIcon or a StatusBar etc. The ImageList stores it's images as System.Drawing.Image objects, but converting them to Icons is easy.

The Bitmap class has a method to return an Icon handle (Hicon). So if you cast the Image as a Bitmap and use the returned Hicon along with the static FromHandle method of the Icon class you can convert the Image to an Icon. Take a look (This example set's the new icon as the icon for a NotifyIcon object in the tray):

notifyIcon1.Icon = Icon.FromHandle(((Bitmap)imageList1.Images[0]).GetHicon());

Have fun.




                   



Leave a comment below.

Comments

  1. S 10/20/2004 4:42 PM
    Gravatar
    Excellent!
  2. SG 1/6/2005 7:27 PM
    Gravatar
    Sweer
  3. ultrasonik 2/1/2005 7:14 AM
    Gravatar
    Works perfect! Thanks!
  4. Ken 2/21/2005 1:23 AM
    Gravatar
    Fantastic!

    Should be more of this; I looked _everywhere_ and only found the answer here.

    Is it just me, or is everything (that _should be intutiave_) hidden from us programmers - just to make us sweat. :)

  5. John 3/6/2005 11:47 AM
    Gravatar
    Thank you!!
  6. Bob 3/7/2005 6:32 AM
    Gravatar
    Excellent, thanks, just saved me some considerable effort ;)
  7. Monty 3/15/2005 4:31 AM
    Gravatar
    P8roblem is it keeps eating memory :-(
  8. newbie 4/14/2005 7:12 AM
    Gravatar
    thanks!
  9. Vam 5/12/2005 3:20 PM
    Gravatar
    Thank you!
  10. eligeti 6/7/2005 6:14 AM
    Gravatar
    thanks. i surfed alot but could not get a reasonable solution until i hit this page
  11. apl 6/12/2005 9:23 AM
    Gravatar
    > P8roblem is it keeps eating memory :-(

    Try to call Dispose() for the Icon...
  12. byounie 7/16/2005 6:08 AM
    Gravatar
    I did this by putting icon files into the image list, and it ends up showing a default icon. When I put a bitmap file into the image list, then it will successfully convert to an icon.

    The only problem with using bitmaps is that you cannot preserve transparency, which I need.

    Is there a way to take an icon file, stick it in an ImageList, then pull it back out into an Icon? Since ImageList takes an Icon, you would think that it would do this out of the box, but apparently it does not.

    Any ideas?
  13. byounie 7/16/2005 6:25 AM
    Gravatar
    Solved it! I had only defined the 32x32 version of the icon, not the 16x16. The fact that it was showing a valid default icon image clued me in.
  14. sicodo 2/21/2006 3:20 AM
    Gravatar
    thank you!
  15. Roger 2/22/2006 1:48 PM
    Gravatar
    Thanks, nice code.
  16. Randy 4/12/2006 5:39 AM
    Gravatar
    i must be doing something wrong cause it wont work for me. im new to VB.NET. but its haveing an error on the bitmap word. it says its a type and cant be used that way. and it says its expecting a ")" after imagelist1
  17. Ryan Farley 4/12/2006 7:14 AM
    Gravatar
    Randy,

    I assume you've translated the C# code I posted to VB.NET? What does it look like (I can't tell you the problem until I see what your VB.NET equivalent looks like).

    The code in the post, translated to VB.NET, would look something like this:

    notifyIcon1.Icon = Icon.FromHandle(CType(imageList1.Images(0), Bitmap).GetHicon())

    Good luck.
  18. Seann 4/13/2006 7:25 PM
    Gravatar
    thanks you!!
  19. Jens 5/13/2006 3:55 AM
    Gravatar
    Perfect!!

    thx
  20. Bobby T. 12/4/2006 9:36 AM
    Gravatar
    I'm having a problem getting transparancy to work for some reason...

    I have created a large collection of icons, and if I manually assign the icon to the notifyicon object it looks normal. But when I use the code here to set the icon it ends up having a dark outline. This is using the exact same icon by first defining it in the notify icon, then running the code to select the icon from the imagelist...

    I have the ImageList object set to 32bit depth, and have the transparent option set properly, but no matter what I try I end up with a dark outline when the code is called...

    I can't for the life of me understand why this is happening...
  21. teb 2/4/2007 1:18 AM
    Gravatar
    cool, dude!
    how the heck did you work that one out?
    thanks anyway
  22. Gabi Braga 2/28/2007 11:10 PM
    Gravatar
    Get By Name

    use imageList1.Images.IndexOfKey("img_name")

    notifyIcon1.Icon = Icon.FromHandle(((Bitmap)imageList1.Images[imageList1.Images.IndexOfKey("img_name")]).GetHicon());
  23. 3/26/2007 10:24 AM
    Gravatar

    blog.iridescence.no : Windows Forms IconAnimator
  24. Murat Doğan Çoruh 4/14/2007 5:12 AM
    Gravatar
    You're wonderful man, that's what i need at this moment :D
  25. host 6/13/2007 11:33 AM
    Gravatar
    Excellent, thanks, just saved me some great efforts.
  26. Wim 6/24/2007 9:56 AM
    Gravatar
    Mmmmmmmmm, wonder how many years you have to be programmer unit you can write code that nice...

    Thanx a lot !
  27. Gert-Jan van der Kamp 11/12/2007 4:00 AM
    Gravatar
    Just googled 'imagelist notifyicon' and yours came up on top, just what i needed! Loads of thanks to you, would have taken me a while to figure this one out!

  28. Coder 11/14/2007 5:06 AM
    Gravatar
    Nice! I am grateful, Thanks!
  29. Erik Palsbo 12/22/2007 11:41 PM
    Gravatar
    I got a problem!
    This works fine - for a while - but then the program cracks with an error.
    Then I made a test program, wher I changed the notificaton icon in a loop.
    After 3316 changes (I gues this number is a matter of available memory?).
    The error messeage is: "A generic error occurred in GDI+."
    Any ideas?
  30. popeye 5/22/2008 2:35 AM
    Gravatar
    Just what i'm looking for. Thansk
  31. Hix 6/12/2008 5:24 AM
    Gravatar
    It was the only solution on the net. Thank you!
  32. viet nam 8/7/2008 12:08 AM
    Gravatar
    thank u very much, :D
  33. TC 9/6/2008 2:24 AM
    Gravatar
    The following code that provide a Image to Icon conversion should solve GDI out of memory problems:

    [DllImport("user32.dll", EntryPoint = "DestroyIcon")]
    private static extern bool DestroyIcon(IntPtr hIcon);

    public static Icon Convert(Image image)
    {
    Icon icon = null;

    Bitmap bitmap = new Bitmap(image);
    IntPtr UnmanagedIconHandle = bitmap.GetHicon();

    // Clone FromHandle result so we can destroy the unmanaged handle version of the icon before the converted object is passed out.
    icon = Icon.FromHandle(UnmanagedIconHandle).Clone() as Icon;

    //Unfortunately, GetHicon creates an unmanaged handle which must be manually destroyed otherwise a generic error will occur in GDI+.
    DestroyIcon(UnmanagedIconHandle);

    return icon;
    }
  34. Ales Prochazka 11/19/2008 2:59 AM
    Gravatar
    Thank you very much, finally it's working :)
  35. 1/21/2009 8:28 PM
    Gravatar
    Extract an icon from an imagelist | keyongtech
  36. Yuvraj 9/21/2009 7:27 AM
    Gravatar
    Thanks. I was searching for how Icon object take ImageList as parameter. It helped me lot.
  37. CLS 12/12/2009 5:34 AM
    Gravatar
    Thanks. You have been bookmarked ;)
  38. TriSys 3/17/2010 9:39 AM
    Gravatar
    Hi Ryan and TC

    My vb.net code was leaking GDI's for each image I converted to an Icon.
    After reading this thread, I fixed my function as follows:

    Public Declare Function DestroyIcon Lib "user32" (ByVal hIcon As IntPtr) As Integer

    Public Function ImageToIcon(ByVal image As System.Drawing.Image) As System.Drawing.Icon
    Try
    ' This leaks memory:
    'Return (System.Drawing.Icon.FromHandle(DirectCast(image, Bitmap).GetHicon()))

    ' Better to break down into 2 stages
    Dim bitmap As New System.Drawing.Bitmap(image)
    Dim UnmanagedIconHandle As IntPtr = bitmap.GetHicon()
    '
    ' Clone FromHandle result so we can destroy the
    unmanaged handle version of the icon before the converted object is passed out.
    Dim myIcon As System.Drawing.Icon = TryCast(System.Drawing.Icon.FromHandle(UnmanagedIconHandle).Clone(), Icon)

    ' Unfortunately, GetHicon creates an unmanaged handle which must be manually destroyed
    ' otherwise a generic error will occur in GDI+.
    DestroyIcon(UnmanagedIconHandle)

    Return myIcon

    Catch ex As Exception
    End Try
    End Function


    Kind regards and thanks for posting your solution,
    TriSys Business Software, Cambridge, UK.
Comments have been closed on this topic.



 

News


Also see my CRM Developer blog

Connect:              


Sponsor

Sections