如何在vc++中轉換HICON到HBITMAP ?

[英]How to convert HICON to HBITMAP in VC++?


How to convert HICON to HBITMAP in VC++?

如何在vc++中轉換HICON到HBITMAP ?

I know this is an FAQ but all the solutions I've found on Google don't work. What I need is a function which takes a parameter HICON and returns HBITMAP.

我知道這是一個常見問題,但是我在谷歌上找到的所有解決方案都不起作用。我需要的是一個函數,它接受參數HICON並返回HBITMAP。

Greatest if possible to make conversion to 32-bit bitmap even the icon is 24-bit, 16-bit or 8-bit.

如果可能的話,最好將轉換為32位位圖,即使圖標是24位、16位或8位。

This is the code, I don't know where it goes wrong:

這是代碼,我不知道哪里出錯了

HBITMAP icon_to_bitmap(HICON Icon_Handle) {
  HDC Screen_Handle = GetDC(NULL);
  HDC Device_Handle = CreateCompatibleDC(Screen_Handle);

  HBITMAP Bitmap_Handle = 
  CreateCompatibleBitmap(Device_Handle,GetSystemMetrics(SM_CXICON),
  GetSystemMetrics(SM_CYICON));

  HBITMAP Old_Bitmap = (HBITMAP)SelectObject(Device_Handle,Bitmap_Handle);
  DrawIcon(Device_Handle, 0,0, Icon_Handle);
  SelectObject(Device_Handle,Old_Bitmap);

  DeleteDC(Device_Handle);
  ReleaseDC(NULL,Screen_Handle);
  return Bitmap_Handle;
}

4 个解决方案

#1


4  

I don't have code readily available to share, but I think this is pretty easy. You have to create the HBITMAP, create a device context, select the bitmap into the DC (this will make the bitmap the drawing area for this DC). Finally call the DrawIcon() function to draw your icon on this DC. After that detach the bitmap from the DC and destroy the DC. Your bitmap now should be ready to go.

我沒有現成的代碼可以共享,但我認為這很簡單。您必須創建HBITMAP,創建一個設備上下文,選擇位圖到DC(這將使位圖成為這個DC的繪圖區)。最后調用DrawIcon()函數來在這個DC上繪制您的圖標。然后將位圖從DC中分離出來並破壞DC。您的位圖現在應該准備好了。

Update after looking at your code:

查看您的代碼后更新:

I believe the problem is in the createCompatibleBitmap call. You are asking for a bitmap compatible with the memory DC, but memory DCs start with a 1 bit/pixel bitmap selected into them. Try asking for a bitmap compatible with the screen DC instead.

我認為問題在於createCompatibleBitmap調用。您需要的是與內存DC兼容的位圖,但是內存DCs從選擇的1位/像素位圖開始。試着要求一個與屏幕DC兼容的位圖。

Update 2: you may want to look at this question as it seems related to your problem.

更新2:你可能想看看這個問題,因為它似乎與你的問題有關。

#2


6  

HDC hDC = GetDC(NULL);
HDC hMemDC = CreateCompatibleDC(hDC);
HBITMAP hMemBmp = CreateCompatibleBitmap(hDC, x, y);
HBITMAP hResultBmp = NULL;
HGDIOBJ hOrgBMP = SelectObject(hMemDC, hMemBmp);

DrawIconEx(hMemDC, 0, 0, hIcon, x, y, 0, NULL, DI_NORMAL);

hResultBmp = hMemBmp;
hMemBmp = NULL;

SelectObject(hMemDC, hOrgBMP);
DeleteDC(hMemDC);
ReleaseDC(NULL, hDC);
DestroyIcon(hIcon);
return hResultBmp;

#3


4  

this code do it:

這段代碼:

HICON hIcon = (HICON)LoadImage(instance, MAKEINTRESOURCEW(IDI_ICON), IMAGE_ICON, width, height, 0);
ICONINFO iconinfo;
GetIconInfo(hIcon, &iconinfo);
HBITMAP hBitmap = iconinfo.hbmColor;

and this is the code in the *.rc file:

這是*中的代碼。rc文件:

IDI_ICON ICON "example.ico"

and this is the code in the *.h file:

這是*中的代碼。h文件:

#define IDI_ICON 4000

#4


2  

I found this(similar code works for me - 32x32 icons with or without alpha data):
  used CopyImage (msdn link)

我發現了這個(類似的代碼對我來說很有用- 32x32的圖標或者沒有alpha數據):使用的CopyImage (msdn鏈接)

HICON hICON = /*your code here*/
HBITMAP hBITMAPcopy;
ICONINFOEX IconInfo;
BITMAP BM_32_bit_color;
BITMAP BM_1_bit_mask;

// 1. From HICON to HBITMAP for color and mask separately
//.cbSize required
//memset((void*)&IconInfo, 0, sizeof(ICONINFOEX));
IconInfo.cbSize = sizeof(ICONINFOEX);
GetIconInfoEx( hICON , &IconInfo);


//HBITMAP IconInfo.hbmColor is 32bit per pxl, however alpha bytes can be zeroed or can be not.
//HBITMAP IconInfo.hbmMask is 1bit per pxl

// 2. From HBITMAP to BITMAP for color
//    (HBITMAP without raw data -> HBITMAP with raw data)
//         LR_CREATEDIBSECTION - DIB section will be created,
//         so .bmBits pointer will not be null
hBITMAPcopy = (HBITMAP)CopyImage(IconInfo.hbmColor, IMAGE_BITMAP, 0, 0, LR_CREATEDIBSECTION);
//    (HBITMAP to BITMAP)
GetObject(hBITMAPcopy, sizeof(BITMAP), &BM_32_bit_color);
//Now: BM_32_bit_color.bmBits pointing to BGRA data.(.bmWidth * .bmHeight * (.bmBitsPixel/8))

// 3. From HBITMAP to BITMAP for mask
hBITMAPcopy = (HBITMAP)CopyImage(IconInfo.hbmMask, IMAGE_BITMAP, 0, 0, LR_CREATEDIBSECTION);
GetObject(hBITMAPcopy, sizeof(BITMAP), &BM_1_bit_mask);
//Now: BM_1_bit_mask.bmBits pointing to mask data (.bmWidth * .bmHeight Bits!)

BM_32_bit_color bitmap may be have Alpha *channel*(each 4th byte) already set! So - check for it before u add mask bit to color data.

BM _32_ bit_color位圖可能有Alpha *channel*(每4個字節)已經設置!因此,在添加蒙版位到顏色數據之前檢查它。


注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2011/09/10/72990319f0dccf915ba0aa21a4977038.html



 
粤ICP备14056181号  © 2014-2020 ITdaan.com