HBITMAP IconToBitmap(HICON hIcon, SIZE* pTargetSize = NULL)
{
ICONINFO info = {0};
if(hIcon == NULL
|| !GetIconInfo(hIcon, &info)
|| !info.fIcon)
{
return NULL;
}
INT nWidth = 0;
INT nHeight = 0;
if(pTargetSize != NULL)
{
nWidth = pTargetSize->cx;
nHeight = pTargetSize->cy;
}
else
{
if(info.hbmColor != NULL)
{
BITMAP bmp = {0};
GetObject(info.hbmColor, sizeof(bmp), &bmp);
nWidth = bmp.bmWidth;
nHeight = bmp.bmHeight;
}
}
if(info.hbmColor != NULL)
{
DeleteObject(info.hbmColor);
info.hbmColor = NULL;
}
if(info.hbmMask != NULL)
{
DeleteObject(info.hbmMask);
info.hbmMask = NULL;
}
if(nWidth <= 0
|| nHeight <= 0)
{
return NULL;
}
INT nPixelCount = nWidth * nHeight;
CDC* pDC = GetDC();
HDC dc = pDC->GetSafeHdc();
INT* pData = NULL;
HDC dcMem = NULL;
HBITMAP hBmpOld = NULL;
bool* pOpaque = NULL;
HBITMAP dib = NULL;
BOOL bSuccess = FALSE;
do
{
BITMAPINFOHEADER bi = {0};
bi.biSize = sizeof(BITMAPINFOHEADER);
bi.biWidth = nWidth;
bi.biHeight = -nHeight;
bi.biPlanes = 1;
bi.biBitCount = 32;
bi.biCompression = BI_RGB;
dib = CreateDIBSection(dc, (BITMAPINFO*)&bi, DIB_RGB_COLORS, (VOID**)&pData, NULL, 0);
if(dib == NULL) break;
memset(pData, 0, nPixelCount * 4);
dcMem = CreateCompatibleDC(dc);
if(dcMem == NULL) break;
hBmpOld = (HBITMAP)SelectObject(dcMem, dib);
::DrawIconEx(dcMem, 0, 0, hIcon, nWidth, nHeight, 0, NULL, DI_MASK);
//pOpaque = new(std::nothrow) bool[nPixelCount];
pOpaque = new bool[nPixelCount];
if(pOpaque == NULL) break;
for (INT i = 0; i < nPixelCount; ++i)
{
pOpaque[i] = !pData[i];
}
memset(pData, 0, nPixelCount * 4);
::DrawIconEx(dcMem, 0, 0, hIcon, nWidth, nHeight, 0, NULL, DI_NORMAL);
BOOL bPixelHasAlpha = FALSE;
UINT* pPixel = (UINT*)pData;
for(INT i = 0; i<nPixelCount; ++i, ++pPixel)
{
if((*pPixel & 0xff000000) != 0)
{
bPixelHasAlpha = TRUE;
break;
}
}
if(!bPixelHasAlpha)
{
pPixel = (UINT*)pData;
for(INT i=0;i <nPixelCount; ++i, ++pPixel)
{
if(pOpaque[i])
{
*pPixel |= 0xFF000000;
}
else
{
*pPixel &= 0x00FFFFFF;
}
}
}
bSuccess = TRUE;
} while(FALSE);
if(pOpaque != NULL)
{
delete []pOpaque;
pOpaque = NULL;
}
if(dcMem != NULL)
{
SelectObject(dcMem, hBmpOld);
DeleteDC(dcMem);
}
ReleaseDC(pDC);
if(!bSuccess)
{
if(dib != NULL)
{
DeleteObject(dib);
dib = NULL;
}
}
return dib;
}