如何将Icon转成Bitmap
1个回答
2014-11-14 · 知道合伙人互联网行家
关注
展开全部
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;
HDC dc = GetDC(NULL);
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];
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(NULL, dc);
if(!bSuccess)
{
if(dib != NULL)
{
DeleteObject(dib);
dib = NULL;
}
}
return dib;
}
{
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;
HDC dc = GetDC(NULL);
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];
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(NULL, dc);
if(!bSuccess)
{
if(dib != NULL)
{
DeleteObject(dib);
dib = NULL;
}
}
return dib;
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
AiPPT
2024-09-19 广告
2024-09-19 广告
随着AI技术的飞速发展,如今市面上涌现了许多实用易操作的AI生成工具1、简介:AiPPT: 这款AI工具智能理解用户输入的主题,提供“AI智能生成”和“导入本地大纲”的选项,生成的PPT内容丰富多样,可自由编辑和添加元素,图表类型包括柱状图...
点击进入详情页
本回答由AiPPT提供
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询