Sign in to follow this  
dali

Get icon of file?

Recommended Posts

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!!!

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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...

Share this post


Link to post
Share on other sites
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).

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this