Dj_Fx8
asked on
HIcon to HBitmap problem
Hi
I have spent several hours looking at other posts here in EE on this but can not get a result. What I'm lookin for the following
BitmapFromIcon(HICON hIcon, HBITMAP hBitmap)
This is what I done
ASSERT(hIcon);
ICONINFO IconInfo;
GetIconInfo(hIcon, &IconInfo);
BITMAP bm;
GetObject(IconInfo.hbmColo r, sizeof(bm), &bm);
HDC hDC= NULL;
HDC dcScreen = ::GetDC(NULL);
hDC = CreateCompatibleDC(dcScree n);
hBitmap = CreateCompatibleBitmap(hDC , bm.bmWidth,bm.bmHeight);
::ReleaseDC(NULL, dcScreen);
HBITMAP bmOld = (HBITMAP)SelectObject(hDC, hBitmap);
DrawIcon(hDC, 0, 0, hIcon);
SelectObject(hDC, bmOld);
I am using this as follows
//
HICON hIcon = ExtractIcon(NULL, szPathName, 0);
ASSERT(hIcon);
HBITMAP hBitmap;
BitmapFromIcon(hIcon, hBitmap);
// I am then using hBitmap is other drawing functions
The problem is I end up with a black square with a white line instead of the icon
Any suggestions
I have spent several hours looking at other posts here in EE on this but can not get a result. What I'm lookin for the following
BitmapFromIcon(HICON hIcon, HBITMAP hBitmap)
This is what I done
ASSERT(hIcon);
ICONINFO IconInfo;
GetIconInfo(hIcon, &IconInfo);
BITMAP bm;
GetObject(IconInfo.hbmColo
HDC hDC= NULL;
HDC dcScreen = ::GetDC(NULL);
hDC = CreateCompatibleDC(dcScree
hBitmap = CreateCompatibleBitmap(hDC
::ReleaseDC(NULL, dcScreen);
HBITMAP bmOld = (HBITMAP)SelectObject(hDC,
DrawIcon(hDC, 0, 0, hIcon);
SelectObject(hDC, bmOld);
I am using this as follows
//
HICON hIcon = ExtractIcon(NULL, szPathName, 0);
ASSERT(hIcon);
HBITMAP hBitmap;
BitmapFromIcon(hIcon, hBitmap);
// I am then using hBitmap is other drawing functions
The problem is I end up with a black square with a white line instead of the icon
Any suggestions
To clarify:
Alternative 1: (easiest)
BitmapFromIcon(HICON hIcon, HBITMAP &hBitmap)
Alternative 2: (more elegant)
HBITMAP BitmapFromIcon(HICON hIcon)
to use as: HBITMAP hBitmap = BitmapFromIcon(hIcon); (function must return bitmap handler)
Alternative 1: (easiest)
BitmapFromIcon(HICON hIcon, HBITMAP &hBitmap)
Alternative 2: (more elegant)
HBITMAP BitmapFromIcon(HICON hIcon)
to use as: HBITMAP hBitmap = BitmapFromIcon(hIcon); (function must return bitmap handler)
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
GetDC(NULL) return the DC from the entire screen.
Read more at:
http://msdn.microsoft.com/library/en-us/gdi/devcons_4esj.asp
Read more at:
http://msdn.microsoft.com/library/en-us/gdi/devcons_4esj.asp
ASKER
Hi migel I understand what your saying the problem is but how do I solve it
What migel said is not exact. GetDC return correct screen DC, not monocrome.
You can make a test:
int bits = GetDeviceCaps(hDC, BITSPIXEL); // will return 24 or 32 bits color resolution
Apparently you are doing thing well inside your function, but YOU ARE NOT RETURNING HBITMAP PROPERLY.
So, after calling BitmapFromIcon(hIcon, hBitmap), hBitmap DOES NOT contain proper handle.
You can make a test:
int bits = GetDeviceCaps(hDC, BITSPIXEL); // will return 24 or 32 bits color resolution
Apparently you are doing thing well inside your function, but YOU ARE NOT RETURNING HBITMAP PROPERLY.
So, after calling BitmapFromIcon(hIcon, hBitmap), hBitmap DOES NOT contain proper handle.
ASKER
>>Apparently you are doing thing well inside your function, but YOU ARE NOT RETURNING HBITMAP PROPERLY.
At the minuite I have not got the code in a seperate function and am using the hBitmap (which is a member var) directly so it's not to do with the returning of the bitmap
I have the same graphic as an icon and a bitmap and when I load the bitmap the rest of my code draws it fine but when I load an icon and convert it to a bitmap it now draws it in black and white (and the scaling of the bitmap that I do later doesn't work on it)
At the minuite I have not got the code in a seperate function and am using the hBitmap (which is a member var) directly so it's not to do with the returning of the bitmap
I have the same graphic as an icon and a bitmap and when I load the bitmap the rest of my code draws it fine but when I load an icon and convert it to a bitmap it now draws it in black and white (and the scaling of the bitmap that I do later doesn't work on it)
to jaime_olivares
You make "common" mistake - memory DC has same characteristiks as refererer but initially has monochorome bitmap selected into it :-) (see MSDN)
Of course if you want return bitmap from function you have to pass variable by the reference not by value so
signature as jaime say must be
int BitmapFromIcon(HICON hIcon, HBITMAP &hBitmap)
or just
HBITMAP BitmapFromIcon(HICON hIcon);
You make "common" mistake - memory DC has same characteristiks as refererer but initially has monochorome bitmap selected into it :-) (see MSDN)
Of course if you want return bitmap from function you have to pass variable by the reference not by value so
signature as jaime say must be
int BitmapFromIcon(HICON hIcon, HBITMAP &hBitmap)
or just
HBITMAP BitmapFromIcon(HICON hIcon);
2) Try to draw your icon on screen to be sure it is correctly loaded
3) Finally, you have not declared function correctly. Can be:
BitmapFromIcon(HICON hIcon, HBITMAP &hBitmap)
HBITMAP BitmapFromIcon(HICON hIcon)
to use as: HBITMAP hBitmap = BitmapFromIcon(hIcon); (function must return bitmap handler)