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:
- Getting the file information, in particular the icon index, for the given file using SHGetFileInfo
- Retrieving the system image list where all the icons are stored
- 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: icon, SHGetFileInfo, SHGetImageList, simple example, Win32
June 10th, 2010 at 18:27
[...] 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 [...]
June 10th, 2010 at 19:01
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
June 14th, 2010 at 14:54
Hi Andrea, glad to hear the code sample helped and that you could convert it to C#
June 23rd, 2011 at 18:30
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