Get icon of file?

Started by
6 comments, last by Evil Steve 16 years, 5 months ago
Hello! I have a strange problem. I just want to read the icon of a file but somehow it does not give me the pixels of the icon. Everything else works fine I can even draw the icon... What could be the problem?

SHFILEINFOA sfi;
SHGetFileInfoA("C:\\test.bmp",FILE_ATTRIBUTE_NORMAL,&sfi,sizeof(sfi),
    SHGFI_ICON | SHGFI_SMALLICON | SHGFI_USEFILEATTRIBUTES);

//DrawIcon(GetDC(NULL), 0,0, sfi.hIcon); // << this works!

DIBSECTION dibSection;
ICONINFO icoInfo;
if(GetIconInfo(sfi.hIcon,&icoInfo) == 0) {
    cout << "Error get info" << endl;
}

BITMAP bm;
GetObject (icoInfo.hbmColor, sizeof (bm), & bm);
cout << "Width: " << bm.bmWidth<<endl; // << correct
cout << "Height: " << bm.bmHeight<<endl; // << correct
cout << "Bits pixel: " << bm.bmBitsPixel<<endl; // << correct
cout << "the pixels: " << bm.bmBits << endl; // wrong result: 0000000
Thank you a lot!!!
Gaudeamus igitur, iuvenes dum sumus!
Advertisement
If I am not mistaken, bm.bmBits only returns an address of bits when the bitmap in question is a DIBSection bitmap. What you are dealing with is just an icon.

What I would do is:

1) Create temporary DC similar to your window DC (using CreateCompatibleDC)
2) Create a DIB section bitmap (using CreateDIBSection)
3) Select DIB section bitmap into your temporary DC (SelectObject) and save the old bitmap of that DC
4) Use DrawIcon to draw icon on the temporary DC
5) Cleanup by selecting temp DC's old bitmap back into it, and by destroying temp DC.

Now you will end up with a DIB section that you can modify the bits of directly. Also notice that an icon has two bitmaps in it: the icon image and the monochrome icon mask. You may have to create two DIB sections, and draw image on one, and mask on another. After you modified the two DIB sections, you can call CreateIcon to once again join them both into one HICON which you can then draw.
Thank you a lot! Is this really the only possibility because this seems not that fast... I just wanted to create a directx texture out of the icons...
Gaudeamus igitur, iuvenes dum sumus!
You can also use GetDIBits.
Thank you but where is the difference between GetObject and GetDIBits?
Gaudeamus igitur, iuvenes dum sumus!
Quote:Original post by dali
Thank you but where is the difference between GetObject and GetDIBits?
GetObject() gets a struct that describes the object (A BITMAP struct in this case). GetDIBits() gets the actual pixel data. GetObject() won't return actual bit data for an object, presumably because it's fairly expensive to do, and usually you only want the other data members (like size).
But in the BITMAP struct you have the value bmBits which is normally pointing to pixels. For example with bitmaps GetObject works but with Icons the bmBits point to nothing... But thank you a lot I will check out GetDIBits.
Gaudeamus igitur, iuvenes dum sumus!
Quote:Original post by dali
But in the BITMAP struct you have the value bmBits which is normally pointing to pixels. For example with bitmaps GetObject works but with Icons the bmBits point to nothing... But thank you a lot I will check out GetDIBits.
The bmBits member doesn't point to pixels in a bitmap when you use GetObject() on a HBITMAP.

This topic is closed to new replies.

Advertisement