不用转为Bitmap,在兼容DC里绘制后再通过算法转为灰度图,网上都有现成的算法了
解决方案 »
- 关于CreateCaret函数用法
- 各位有用PDFLIB 或者其他非打印方式,实现word---pdf转换的吗?
- 天哪!VC案例竟是病毒!
- 请大家帮我罗列多线程间协同工作的函数
- 如何读取网页中特定的内容?
- 在线等待:一个CScrollView的问题
- 单文档/视程序,怎样使它在被关闭时不提示是否保存文档而直接保存后退出?
- 在线求救,位图存取?????
- 谁能介绍一下学vc++6.0的好书
- 为什么我directshow开发的 声音和图象都有很大的延迟,而且随时间图象就会卡住??谢谢
- 求助skinmagic builder 界面工具修改client 客户区背景,在测试时可以显示,但在MFC程序就不会显示了
- 当在鼠标右键按下去时用GetClaaaLong改变光标
GetIconInfo()/CopyImage(info.hbmColor, IMAGE_BITMAP,...);
用GDI+估计几行代码吧 效果如何 就没试验过了
关键代码
Bitmap *pBmp = Bitmap::FromHICON(hIcon);
ImageAttributes imAtt;
imAtt.SetOutputChannel( ColorChannelFlagsK, ColorAdjustTypeDefault);Graphics g(pBmp);
pg->DrawImage( pBmp, RectF(0,0,pBmp->GetWidth(),pBmp->GetHeight()) ,0, 0,
pBmp->GetWidth(),
pBmp->GetHeight(),
UnitPixel, &imAtt );
以上代码获得的pBmp为灰度图
在使用CreateIcon获得灰图标
#ifndef _CREATEGRAYSCALEICON_H
#define _CREATEGRAYSCALEICON_HCOLORREF defaultGrayPalette[256];
BOOL bGrayPaletteSet = FALSE;HICON CreateGrayscaleIcon( HICON hIcon, COLORREF* pPalette )
{
if (hIcon == NULL)
{
return NULL;
} HDC hdc = ::GetDC(NULL); HICON hGrayIcon = NULL;
ICONINFO icInfo = { 0 };
ICONINFO icGrayInfo = { 0 };
LPDWORD lpBits = NULL;
LPBYTE lpBitsPtr = NULL;
SIZE sz;
DWORD c1 = 0;
BITMAPINFO bmpInfo = { 0 };
bmpInfo.bmiHeader.biSize = sizeof(BITMAPINFOHEADER); if (::GetIconInfo(hIcon, &icInfo))
{
if (::GetDIBits(hdc, icInfo.hbmColor, 0, 0, NULL, &bmpInfo, DIB_RGB_COLORS) != 0)
{
bmpInfo.bmiHeader.biCompression = BI_RGB; sz.cx = bmpInfo.bmiHeader.biWidth;
sz.cy = bmpInfo.bmiHeader.biHeight;
c1 = sz.cx * sz.cy; lpBits = (LPDWORD)::GlobalAlloc(GMEM_FIXED, (c1) * 4); if (lpBits && ::GetDIBits(hdc, icInfo.hbmColor, 0, sz.cy, lpBits, &bmpInfo, DIB_RGB_COLORS) != 0)
{
lpBitsPtr = (LPBYTE)lpBits;
UINT off = 0; for (UINT i = 0; i < c1; i++)
{
off = (UINT)( 255 - (( lpBitsPtr[0] + lpBitsPtr[1] + lpBitsPtr[2] ) / 3) ); if (lpBitsPtr[3] != 0 || off != 255)
{
if (off == 0)
{
off = 1;
} lpBits[i] = pPalette[off] | ( lpBitsPtr[3] << 24 );
} lpBitsPtr += 4;
} icGrayInfo.hbmColor = ::CreateCompatibleBitmap(hdc, sz.cx, sz.cy); if (icGrayInfo.hbmColor != NULL)
{
::SetDIBits(hdc, icGrayInfo.hbmColor, 0, sz.cy, lpBits, &bmpInfo, DIB_RGB_COLORS); icGrayInfo.hbmMask = icInfo.hbmMask;
icGrayInfo.fIcon = TRUE; hGrayIcon = ::CreateIconIndirect(&icGrayInfo); ::DeleteObject(icGrayInfo.hbmColor);
} ::GlobalFree(lpBits);
lpBits = NULL;
}
} ::DeleteObject(icInfo.hbmColor);
::DeleteObject(icInfo.hbmMask);
} ::ReleaseDC(NULL,hdc); return hGrayIcon;
}HICON CreateGrayscaleIcon( HICON hIcon )
{
if (hIcon == NULL)
{
return NULL;
} if (!bGrayPaletteSet)
{
for(int i = 0; i < 256; i++)
{
defaultGrayPalette[i] = RGB(255-i, 255-i, 255-i);
} bGrayPaletteSet = TRUE;
} return CreateGrayscaleIcon(hIcon, defaultGrayPalette);
}
#endif