Getting 48X48 Icons

ExtractIconEX will allow you to get large and small icons from an ICO file.

However, this only works on the 32X32 and 16X16 size icons.

What do I use to get the 48X48 icons and how do I tell HOW MANY icons are in the file?
LVL 26
Eddie ShipmanAll-around developerAsked:
Who is Participating?
Slick812Connect With a Mentor Commented:
If you only need   Ico  file 48 size icons, you might try this code, the  hIconL  will be the Icon Handle for a 48 size Icon

procedure TForm1.button_Get48hIconClick(Sender: TObject);
IcoFilePath: String;
DeskTopISF: IShellFolder;
PathPidl: PItemIDList;
IEIcon: IExtractIcon;
hIconL, hIconS: HICON;
  this was made to ONLY work with  .ICO  files}
IcoFilePath := 'E:\ico folder\zTes3.ico';
if (not FileExists(IcoFilePath)) or
  (LowerCase(ExtractFileExt(IcoFilePath)) <> '.ico') then
  ShowMessage('ERROR = BAD Icon File Name');

if SHGetDesktopFolder(DeskTopISF) <> NOERROR then
  ShowMessage('ERROR = SHGetDesktopFolder FAILURE');

PathPidl := nil;
{this DOES NOT get any Shell Interface for the Icon File}
if DeskTopISF.GetUIObjectOf(Handle,1, PathPidl,IID_IExtractIconA, nil, IEIcon) <> NOERROR then
  ShowMessage('ERROR = DeskTopISF.GetUIObjectOf  FAILED');

  {I just use the Icon file path with Index Zero, there is NO Shell Interface Object,
    so this will NOT work on files that do NOT have icons in them, like .txt  or  .wav }
if IEIcon.Extract(PChar(IcoFilePath),0,hIconL, hIconS, 48 or (16 shl 16)) = NOERROR then
  DrawIconEx(Canvas.Handle,60,49, hIconL, 0, 0, 0, 0, DI_NORMAL) else
    ShowMessage('FAILURE - IExIcon Extract');
I don't know how much is it correct, but to detect icon count I'm using this code.
It seems to work fine.

  while ExtractIconEx(PChar(ComboBox1.Items[ComboBox1.ItemIndex]),
            Index, LargeIconHandle,SmallIconHandle, 1) = 2 do

u want to make such a program like this one
Cloud Class® Course: CompTIA Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

Eddie ShipmanAll-around developerAuthor Commented:
mokule: I don't see how that returns the NUMBER of icons in the file.

waldin: No, just want to be able to LOAD the correct icon.
Sorry for some mess
Should be
ExtractIconEx('filename', -1, LargeIconHandle,SmallIconHandle, 1)

Return Value

If the nIconIndex parameter is -1, the phiconLarge parameter is NULL, and the phiconSmall parameter is NULL, then the return value is the number of icons contained in the specified file. Otherwise, the return value is the number of icons successfully extracted from the file.

Eddie ShipmanAll-around developerAuthor Commented:
Yes, passing null, null, -1 to ExtractIconEx does return the number.

Now, from what I'm reading, you must use LoadImage to load a 48X48 icon, it seems.

Any comments?

"There are two ways to retrieve an object's icon. The simplest way is to call SHGetFileInfo. However, this approach is inflexible and may be rather slow. A more flexible and efficient way to retrieve an item's icon is to use IExtractIcon. The Shell uses IExtractIcon to retrieve icons when it displays the contents of a folder. To use IExtractIcon to retrieve an object's icon:

   1. Get a pointer to the IShellFolder interface of the folder that contains the object.
   2. Call IShellFolder::GetUIObjectOf with the pointer to an item identifier list (PIDL) of the object and the interface identifier (ID) of IExtractIcon (IID_IExtractIcon). The folder creates an object to handle the icon extraction, and returns the object's IExtractIcon interface pointer.
   3. Call IExtractIcon::GetIconLocation to retrieve the icon's location.
   4. Call IExtractIcon::Extract to retrieve the icon's handle."
Eddie ShipmanAll-around developerAuthor Commented:
Sorry, but it is from an icon FILE, not an executable.
hello  EddieShipman,   If you are asking about getting icon information from Icon Files  .ICO  , you may have to read the Icon file headers, to getthis information.
 the first  Six bytes of th icon file is the  main icon file header, and looks like this -

TIconCurHeader = packed record
    wReserved: Word;
    wType: Word;  // has icon or cursor
    wCount: Word;  // has the number of individual bitmaps for icons in the file

You can use TFileStream or the delphi File to read the fifth and sixth bytes of the file as a word to get the number of icons in the file

after these header six bytes, there are the Icon headers which look like this -

  TIconSpec = packed record
    iWidth: Byte;
    iHeight: Byte;
    iColors: Word;
    Reserved1: Word;
    Reserved2: Word;
    DIBSize: Longint;
    DIBOffset: Longint;

there is one of these headers for every icon in the file

you can read the first or second byte to get the width or height
not all Icon  files have a  48x48  size icon in them, any icons the system needs that are not in the icon file, it creates

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.