Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

How do you convert a HICON to HBITMAP

Posted on 1999-08-03
18
Medium Priority
?
743 Views
Last Modified: 2013-12-03
I need the following function:

HBITMAP IconToBitmap(HICON hIcon);
0
Comment
Question by:jmayfield
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 11
  • 6
18 Comments
 
LVL 23

Expert Comment

by:chensu
ID: 1404503
1. Create a memory DC (CreateCompatibleDC).
2. Create a bitmap (CreateCompatibleBitmap).
3. Select the bitmap into the memory DC.
4. Draw the icon (DrawIcon).
5. Deselect the bitmap.
6. Delete the DC.
7. Return the bitmap handle.
0
 
LVL 23

Expert Comment

by:chensu
ID: 1404504
This technical article may help.

Icons in Win32
http://msdn.microsoft.com/library/techart/msdn_icons.htm
0
 

Author Comment

by:jmayfield
ID: 1404505
Out of curiosity, GetObject returns the size of a bitmap. Does GetObject also return the size of an icon?

Also, how do you deselect an object after using SelectObject?
0
Veeam Task Manager for Hyper-V

Task Manager for Hyper-V provides critical information that allows you to monitor Hyper-V performance by displaying real-time views of CPU and memory at the individual VM-level, so you can quickly identify which VMs are using host resources.

 

Author Comment

by:jmayfield
ID: 1404506
I'm going to give you the points, but I'm just coding it up now in case I have any problems.

Thanks for the prompt answer and excellent info.
0
 

Author Comment

by:jmayfield
ID: 1404507
I'm going to give you the points, but I'm just coding it up now in case I have any problems.

Thanks for the prompt answer and excellent info.
0
 

Author Comment

by:jmayfield
ID: 1404508
I'm going to give you the points, but I'm just coding it up now in case I have any problems.

Thanks for the prompt answer and excellent info.
0
 

Author Comment

by:jmayfield
ID: 1404509
I'm going to give you the points, but I'm just coding it up now in case I have any problems.

Thanks for the prompt answer and excellent info.
0
 

Author Comment

by:jmayfield
ID: 1404510
I'm going to give you the points, but I'm just coding it up now in case I have any problems.

Thanks for the prompt answer and excellent info.
0
 
LVL 23

Expert Comment

by:chensu
ID: 1404511
>Does GetObject also return the size of an icon?

No, it doesn't. But, there is a way to get the size of an icon. Use GetIconInfo, which returns HBITMAP hbmMask. Then use GetObject on the hbmMask.

>how do you deselect an object after using SelectObject?

HBITMAP hOldBmp = SelectObject(hdc, hBmp);

//...

SelectObject(hdc, hOldBmp);
0
 

Author Comment

by:jmayfield
ID: 1404512
Nice steps above, but what is wrong with the following code?

HBITMAP IconToBitmap(HICON hIcon,int Width,int Height)
{
  if(!hIcon) return NULL;

  HBITMAP hBitmap = NULL;
  HBITMAP hOldBitmap = NULL;
  HDC hDC = NULL;
  HDC ScreenDC = GetDC(NULL);

  hDC = CreateCompatibleDC(ScreenDC);
  ReleaseDC(NULL,ScreenDC);
  if(!hDC)
  {
    MessageBoxDebug("Failed on CreateCompatibleDC");
    return NULL;
  }

  hBitmap = CreateCompatibleBitmap(hDC,
                                   Width,
                                   Height);
  if(!hBitmap)
  {
    MessageBoxDebug("Failed on CreateCompatibleBitmap");
    ReleaseDC(NULL,hDC);
    return NULL;
  }

  hOldBitmap = (HBITMAP)SelectObject(hDC,hBitmap);
  if(!DrawIcon(hDC,0,0,hIcon))
  {
    MessageBoxDebug("Failed on DrawIcon");
    ReleaseDC(NULL,hDC);
    return NULL;
  }

  SelectObject(hDC,hOldBitmap);

  ReleaseDC(NULL,hDC);

  return hBitmap;
}

0
 

Author Comment

by:jmayfield
ID: 1404513
Nice steps above, but what is wrong with the following code?

HBITMAP IconToBitmap(HICON hIcon,int Width,int Height)
{
  if(!hIcon) return NULL;

  HBITMAP hBitmap = NULL;
  HBITMAP hOldBitmap = NULL;
  HDC hDC = NULL;
  HDC ScreenDC = GetDC(NULL);

  hDC = CreateCompatibleDC(ScreenDC);
  ReleaseDC(NULL,ScreenDC);
  if(!hDC)
  {
    MessageBoxDebug("Failed on CreateCompatibleDC");
    return NULL;
  }

  hBitmap = CreateCompatibleBitmap(hDC,
                                   Width,
                                   Height);
  if(!hBitmap)
  {
    MessageBoxDebug("Failed on CreateCompatibleBitmap");
    ReleaseDC(NULL,hDC);
    return NULL;
  }

  hOldBitmap = (HBITMAP)SelectObject(hDC,hBitmap);
  if(!DrawIcon(hDC,0,0,hIcon))
  {
    MessageBoxDebug("Failed on DrawIcon");
    ReleaseDC(NULL,hDC);
    return NULL;
  }

  SelectObject(hDC,hOldBitmap);

  ReleaseDC(NULL,hDC);

  return hBitmap;
}

0
 
LVL 23

Expert Comment

by:chensu
ID: 1404514
 //...

  hDC = CreateCompatibleDC(ScreenDC);
   
  if(!hDC)
  {
    MessageBoxDebug("Failed on CreateCompatibleDC");
    return NULL;
  }

  hBitmap = CreateCompatibleBitmap(ScreenDC,
                                   Width,
                                   Height);

  ReleaseDC(NULL,ScreenDC);

  //...

  DeleteDC(hDC);

  //...
0
 

Author Comment

by:jmayfield
ID: 1404515
Still no go. I get a black bitmap with a few white dots. I have also tried DrawIconEx. It draws a little different black square with different white dots. CreateCompatibleBitmap from the ScreenDC did not change anything. Any other ideas?
0
 
LVL 2

Expert Comment

by:The Master
ID: 1404516
That function should work, as long as the icon handle is ok.  How are you getting the icon handle?

When you create a new bitmap, it will initially be filled with random bits.  If your icon has any transparent parts, you will want to set the background color to the color of your choice.  If white or black, you could do something similar to the following:  PatBlt (hDC, 0, 0, Width, Height, WHITENESS);

You may also want to provide your own DC to the function to make sure the icon is ok.  You could do something like this in a test window's WM_PAINT message:

case WM_PAINT:
   PAINTSTRUCT ps;
   HDC hDC = BeginPaint (hWnd, &ps);
   HBITMAP hBmp = IconToBmp (hDC, hIcon, cxIcon, cyIcon);
   HDC hMemDC = CreateCompatibleDC (hDC);
   HBITMAP hOldMemBmp = SelectObject (hMemDC, hBmp);
   
   BitBlt (hDC, 10, 10, cxIcon, cyIcon, hMemDC, 0, 0, SRCCOPY);

   SelectObject (hMemDC, hOldMemBmp);
   DeleteDC (hMemDC);
   DeleteObject (hBmp);

   DrawIcon (hDC, 50, 10, hIcon);

   EndPaint (hWnd, &ps);
   break;
0
 

Author Comment

by:jmayfield
ID: 1404517
I got it to work. Two things must be added.

1. FillRect(hDC,&Rect,GetSysColorBrush(COLOR_WINDOW));
2. Must use DrawIconEx.

chensu, if you propose an answer again, I will give you the points.

Thank you very much for the steps.

0
 
LVL 23

Expert Comment

by:chensu
ID: 1404518
1. CreateCompatibleBitmap must use ScreenDC.

2. Is the icon 256-color or 16-color?

3. Try DrawIcon to the screen.
0
 
LVL 23

Accepted Solution

by:
chensu earned 1500 total points
ID: 1404519
Sorry, I forgot the important step painting the bitmap.

Thank you.
0
 

Author Comment

by:jmayfield
ID: 1404520
There is also a second way of doing this.

1. GetIconInfo
2. Use the ICONINFO hbmMask and hbmColor to copy it onto a DC.


0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This article describes how to add a user-defined command button to the Windows 7 Explorer toolbar.  In the previous article (http://www.experts-exchange.com/A_2172.html), we saw how to put the Delete button back there where it belongs.  "Delete" is …
If you have ever found yourself doing a repetitive action with the mouse and keyboard, and if you have even a little programming experience, there is a good chance that you can use a text editor to whip together a sort of macro to automate the proce…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…

688 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