Get the 48×48 or 256×256 icon of a file on Windows

Getting the 16×16 and 32×32 icons on Windows is relatively easy and is often as simple as one call to ExtractIconEx.

However, getting the extra large (48×48) and jumbo (256×256) icons introduced respectively by XP and Vista is slighly more complex. This is normally done by:

  1. Getting the file information, in particular the icon index, for the given file using SHGetFileInfo
  2. Retrieving the system image list where all the icons are stored
  3. Casting the image list to an IImageList interface and getting the icon from there

Below is the code I’m using in Appetizer to retrieve the extra large icon. If needed it can easily be adapted to get the jumbo icon.

Update: To do the same thing in C#, see the link in the comments below.

#include <shlobj.h>
#include <shlguid.h>
#include <shellapi.h>
#include <commctrl.h>
#include <commoncontrols.h>

// Get the icon index using SHGetFileInfo
SHFILEINFOW sfi = {0};
SHGetFileInfo(filePath, -1, &sfi, sizeof(sfi), SHGFI_SYSICONINDEX);

// Retrieve the system image list.
// To get the 48x48 icons, use SHIL_EXTRALARGE
// To get the 256x256 icons (Vista only), use SHIL_JUMBO
HIMAGELIST* imageList;
HRESULT hResult = SHGetImageList(SHIL_EXTRALARGE, IID_IImageList, (void**)&imageList);

if (hResult == S_OK) {
  // Get the icon we need from the list. Note that the HIMAGELIST we retrieved
  // earlier needs to be casted to the IImageList interface before use.
  HICON hIcon;
  hResult = ((IImageList*)imageList)->GetIcon(sfi.iIcon, ILD_TRANSPARENT, &hIcon);

  if (hResult == S_OK) {
    // Do something with the icon here.
    // For example, in wxWidgets:
    wxIcon* icon = new wxIcon();
    icon->SetHICON((WXHICON)hIcon);
    icon->SetSize(48, 48);
  }
}

Tags: , , , ,

7 Responses to “Get the 48×48 or 256×256 icon of a file on Windows”

  1. How to have large file icons with SHGetFileInfo in C# | Tabbles developers' blog Says:

    […] we started investigating and found this article on how to get large icons using C++. The problem was to convert this code to C#. This is not easy, at least for me, because it […]

  2. Andrea D'Intino Says:

    Hi there,
    thanks for the article, we used it along with an other article to implement large icons in our .net application. We wrote a post about the whole story here:

    http://tabbles.net/blog/2010/06/10/how-to-have-large-file-icons-with-shgetfileinfo-in-c/

    Thanks 🙂

    Andrea and Maurizio

  3. Laurent Says:

    Hi Andrea, glad to hear the code sample helped and that you could convert it to C# 🙂

  4. JKR Says:

    it doesnt work for me at all…

    SHGetFileInfo returns NULL
    and sfi is empty.

    compiled as 32bit on a 64bit machine
    do you have any idea how to solve this?

    Regards
    JKR

  5. CHP Says:

    You need to use File Attributes:

    SHGetFileInfo(…,FILE_ATTRIBUTE_NORMAL,…,SHGFI_SYSICONINDEX|SHGFI_USEFILEATTRIBUTES);

  6. bai Says:

    where is link for c#? send to me .thank you very much. oba…(korea)

  7. Johnb140 Says:

    Wonderful blog! I found it while surfing around on Yahoo News. Do you have any suggestions on how to get listed in Yahoo News? I’ve been trying for a while but I never seem to get there! Thanks eadedfgdddff

Copyright © Pogopixels Ltd, 2008-2018