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

  pogopixels.com        2012-03-23 09:35:17       9,991        0    

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.

  1. #include <shlobj.h> 
  2. #include <shlguid.h> 
  3. #include <shellapi.h> 
  4. #include <commctrl.h> 
  5. #include <commoncontrols.h> 
  6.  
  7. // Get the icon index using SHGetFileInfo 
  8. SHFILEINFOW sfi = {0}; 
  9. SHGetFileInfo(filePath, -1, &sfi, sizeof(sfi), SHGFI_SYSICONINDEX); 
  10.  
  11. // Retrieve the system image list. 
  12. // To get the 48x48 icons, use SHIL_EXTRALARGE 
  13. // To get the 256x256 icons (Vista only), use SHIL_JUMBO 
  14. HIMAGELIST* imageList; 
  15. HRESULT hResult = SHGetImageList(SHIL_EXTRALARGE, IID_IImageList, (void**)&imageList); 
  16.  
  17. if (hResult == S_OK) { 
  18.   // Get the icon we need from the list. Note that the HIMAGELIST we retrieved 
  19.   // earlier needs to be casted to the IImageList interface before use. 
  20.   HICON hIcon; 
  21.   hResult = ((IImageList*)imageList)->GetIcon(sfi.iIcon, ILD_TRANSPARENT, &hIcon); 
  22.  
  23.   if (hResult == S_OK) { 
  24.     // Do something with the icon here. 
  25.     // For example, in wxWidgets: 
  26.     wxIcon* icon = new wxIcon(); 
  27.     icon->SetHICON((WXHICON)hIcon); 
  28.     icon->SetSize(48, 48); 
  29.   } 

Source:http://pogopixels.com/blog/getting-the-48x48-or-256x256-icon-of-a-file-on-windows/

FILE ICON  FILE EXTENSION  ASSOCIATION  LARGE ICON  WINDOWS 

       

  RELATED


  0 COMMENT


No comment for this article.



  RANDOM FUN

What a wrong workaround