By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,538 Members | 2,225 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,538 IT Pros & Developers. It's quick & easy.

Extract an icon from an imagelist

P: n/a
Udi
Hi,
I have an imagelist initialized in design time with
several icons files (.ico).
However, when I need to extract an icon (Image)
from the Imagelist, I can't convert it back to an icon:

statusBarPanel1.Icon = imageList1.Images[0]; // cast/as don't work
// Cannot convert type 'System.Drawing.Image' to 'System.Drawing.Icon'

What am I mising here?
Thanks!
Udi.

Jan 24 '06 #1
Share this Question
Share on Google+
6 Replies


P: n/a
Udi,

Unfortunately getting an icon from an image list is a function that is
not exposed in the managed class. However, you can get the icon yourself,
by calling the ImageList_GetIcon function through the P/Invoke layer. You
can pass the handle of the imagelist (exposed by the HandleProperty) to the
function and it will return a handle, which you can then pass to the static
FromHandle property on the Icon class.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Udi" <Ud**********@gmail.com> wrote in message
news:11**********************@f14g2000cwb.googlegr oups.com...
Hi,
I have an imagelist initialized in design time with
several icons files (.ico).
However, when I need to extract an icon (Image)
from the Imagelist, I can't convert it back to an icon:

statusBarPanel1.Icon = imageList1.Images[0]; // cast/as don't work
// Cannot convert type 'System.Drawing.Image' to 'System.Drawing.Icon'

What am I mising here?
Thanks!
Udi.

Jan 24 '06 #2

P: n/a
>From http://ryanfarley.com/blog/archive/2004/04/06/507.aspx :

this.statusBar1.Panels[0].Icon =
Icon.FromHandle(((Bitmap)imageList1.Images[0]).GetHicon());

Jan 24 '06 #3

P: n/a
Udi
Thanks Nicholas,
But in which dll resides ImageList_GetIcon() ?
do you have any example of it?
Thanks again!
Udi.

Jan 24 '06 #4

P: n/a
Udi,

You will need to use the P/Invoke layer to make the call. You have to
declare the function like this:

[DllImport("comctl32.dll", CharSet=CharSet.Auto)]
static extern IntPtr ImageList_GetIcon(IntPtr himl, int i,
[MarshalAs(UnmanagedType.U4)] int flags);
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Udi" <Ud**********@gmail.com> wrote in message
news:11**********************@g49g2000cwa.googlegr oups.com...
Thanks Nicholas,
But in which dll resides ImageList_GetIcon() ?
do you have any example of it?
Thanks again!
Udi.

Jan 24 '06 #5

P: n/a
The code I posted above works fine for me

Jan 24 '06 #6

P: n/a
Udi
Yup, Chris, for me as well!
Thanks you all!
Udi.

Jan 24 '06 #7

This discussion thread is closed

Replies have been disabled for this discussion.