views:

138

answers:

1

Hi

How can I extract a image of specific size or all images from an Icon with multiple images?

TIA and best regards

+2  A: 

You don't say what the purpose of this is, and there are a number of ways of getting icons and one method may be better than another depending on the purpose.

However, to extract any number of "large" (32x32) and/or "small" (16x16) icons* from an arbitrary file, use the ExtractIconEx function. This lets you extract one or more icons of either of these two standard sizes from an icon file (.ico), EXE, or DLL. An icon file can contain multiple images and this function will let you get all of them.

The function will write to an array of handles, each of which you can assign to the Handle property of a TIcon instance. Then you can use the TIcon methods as you normally would if you'd loaded the TIcon any other way. Note that new to Delphi XE is the ability to copy it easily to a bitmap via the Assign method. That article also shows how to access all of the stock (inbuilt / standard) Windows icons, if that happens to be what you're after.

(Side note: I think the TIcon class lets you load from a file via its LoadFromFile method - this seems to be missing from the documentation, but I'm pretty certain it exists. From memory, that only loads a single icon.)

(*) Actually, "large" and "small" can be different to 32x32 and 16x16: use the GetSystemMetrics function with the SM_CXICON, SM_CYICON, SM_CXSMICON, and SM_CYSMICON flags to find out the dimensions of each type.

David M
@David, thank you! I have TIcon object and I need to extract all images from it. Is there any way to do that without save icon to file via SaveToFile method?
Branko
TIcon will be one icon image. Using `ExtractIconEx` you would create one TIcon per icon image that it extracts. You can then access the image of the icon by `Assign`-ing it to a bitmap. (Did you get an array of handles filled? Try calling `ExtractIconEx` twice, the first time with `nIconIndex` set to -1 and both array parameters `nil`, and it will return the number of icons. Call it again with an array of that size. See the MSDN article for it for more details.)
David M
@David, TIcon is one image icon if it is obtained by ExtractIconEx, but what if existing TIcon object has more than one image. If I save TIcon with 4 images to file (oIcon.SaveToFile) and open this file with IconEditor I can see all 4 images and I CAN extract all 4 images with ExtractIconEx. Is it possible to extract all images from TIcon object, not to save TIcon to file and use ExtractIconEx?
Branko
How do you get the TIcon with multiple images in the first place? If it comes from a file at some point, you can use ExtractIconEx. If it doesn't, how do you make or find it? I'm a bit confused about what you have, I'm afraid...
David M
In Graphics.pas, there's a method ReadIcon that parses an icon memory image from a stream and extracts one icon of the best size from it. You could use that code as a starting point (be aware of copyright, use it to learn the technique) but make the choice of which image to extract on different criteria.
David M
@David, that's what I was looking for. Thank you!!!
Branko