Solved

HIcon to HBitmap problem

Posted on 2004-08-08
11
1,485 Views
Last Modified: 2013-11-20
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.hbmColor, sizeof(bm), &bm);

HDC hDC= NULL;
HDC dcScreen = ::GetDC(NULL);
hDC = CreateCompatibleDC(dcScreen);
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

0
Comment
Question by:Dj_Fx8
  • 4
  • 2
  • 2
11 Comments
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 11746811
1)  Are you sure bitmap dimensions are ok?
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)

0
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 11746821
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)


0
 
LVL 12

Accepted Solution

by:
migel earned 500 total points
ID: 11746926
Just for error understanding:
here is mistake

HDC hDC= NULL;
HDC dcScreen = ::GetDC(NULL);
hDC = CreateCompatibleDC(dcScreen);
hBitmap = CreateCompatibleBitmap(dcScreen, bm.bmWidth,bm.bmHeight); << HERE!

since memory dc initially have monochrome bitmap you create another monochrome bitmap not colored as you expected :-)
0
Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 11746950
GetDC(NULL) return the DC from the entire screen.
Read more at:
http://msdn.microsoft.com/library/en-us/gdi/devcons_4esj.asp
0
 

Author Comment

by:Dj_Fx8
ID: 11747434
Hi migel I understand what your saying the problem is but how do I solve it
0
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 11747540
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.
0
 

Author Comment

by:Dj_Fx8
ID: 11747620
>>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)
0
 
LVL 12

Expert Comment

by:migel
ID: 11750283
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);
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
iSeries FTP Exit Program 8 135
iSeries DB2 Query 2 95
Problem to event 3 92
Trying to run powershell  in a batch file. How do I do this? 8 89
Introduction: Load and Save to file, Document-View interaction inside the SDI. Continuing from the second article about sudoku.   Open the project in visual studio. From the class view select CSudokuDoc and double click to open the header …
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…

813 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now