Extract an icon from an imagelist

U

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.
 
N

Nicholas Paldino [.NET/C# MVP]

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.
 
U

Udi

Thanks Nicholas,
But in which dll resides ImageList_GetIcon() ?
do you have any example of it?
Thanks again!
Udi.
 
N

Nicholas Paldino [.NET/C# MVP]

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top