Jump to content
  • Advertisement
Sign in to follow this  
dali

Get icon of file?

This topic is 3896 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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

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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!