1

I want to load the icon of an .exe in a CImage object, in order to easily display and save it in a database. My problem is that I have an HICON and I seem to only be able to load/attach an HBITMAP to a CImage object.

SHFILEINFO sfi;
Cimage icon; 

DWORD ret = SHGetFileInfo(path, 0, &sfi, sizeof(SHFILEINFO), SHGFI_ICON | SHGFI_SMALLICON | SHGFI_DISPLAYNAME);
if(ret != 0) {
    // copy info
    hIcon = sfi.hIcon;
    if(hIcon != lastHIcon && hIcon != 0) {
        // This does not work, needs a HBITMAP instead of a HICON
        icon.Attach(hIcon);
    }
}

Is there any easy method of doing this, or do I need to render the HICON to a DC first? It just seems to me that there should be a solution that does not require me to be a GDI expert.

  • Convert it to a HBITMAP. http://stackoverflow.com/questions/7375003/how-to-convert-hicon-to-hbitmap-in-vc – Eejin Feb 25 '14 at 23:13
  • @Eejin: isn't there a way of loading it without the first having to convert it to HBITMAP using a HDC? –  Feb 25 '14 at 23:15
  • Is it realy necessary to directly load as a bitmap? Otherwise, I do not know of a way. – Eejin Feb 25 '14 at 23:37
  • 1
    `GetIconInfo[Ex]` extracts two bitmaps from an icon - the color image and the transparency mask. That doesn't require an `HDC` – Igor Tandetnik Feb 26 '14 at 01:10

0 Answers0