471,351 Members | 1,468 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,351 software developers and data experts.

Extract an icon from an imagelist

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
6 10561
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
>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
Udi
Thanks Nicholas,
But in which dll resides ImageList_GetIcon() ?
do you have any example of it?
Thanks again!
Udi.

Jan 24 '06 #4
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
The code I posted above works fine for me

Jan 24 '06 #6
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.

Similar topics

6 posts views Thread by Mohammad-Reza | last post: by
3 posts views Thread by Ravi Shankar | last post: by
8 posts views Thread by Dinesh Jain | last post: by
2 posts views Thread by Spam Catcher | last post: by
7 posts views Thread by =?Utf-8?B?QmVu?= | last post: by
reply views Thread by Sathyaish | last post: by
reply views Thread by XIAOLAOHU | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.