这段代码作用是把hDIB转成GDI API可以直接操作的BITMAP对象
有个问题 我传入的hDIB明明是256色 也就是8 bpp的 为什么
它反给我的BITMAP会变成我当前桌面的色深?我桌面32它也32
16 它也16 !! 为什么不是我要的256色 or 8 bpp??
各位大虾看能不能修改一下这代码 把问题给纠正了
谢谢
HBITMAP BitmapFromDIB (HANDLE hDIB, HPALETTE hPal)
{
LPBITMAPINFOHEADER lpbih;
HPALETTE hPalOld;
HDC hDC;
HBITMAP hBitmap;
if (!hDIB)
return NULL; lpbih = (LPBITMAPINFOHEADER)GlobalLock(hDIB); if (!lpbih)
return NULL; hDC = GetDC(NULL); if (hPal){
hPalOld = SelectPalette(hDC, hPal, FALSE);
RealizePalette(hDC);
}
hBitmap = CreateDIBitmap(hDC,
lpbih,
CBM_INIT,
(LPSTR)lpbih + lpbih->biSize + ColorTableSize(lpbih),
(LPBITMAPINFO)lpbih,
DIB_RGB_COLORS ); if (hPal)
SelectPalette(hDC, hPalOld, FALSE); ReleaseDC(NULL, hDC);
GlobalUnlock(hDIB);
return hBitmap;
}
有个问题 我传入的hDIB明明是256色 也就是8 bpp的 为什么
它反给我的BITMAP会变成我当前桌面的色深?我桌面32它也32
16 它也16 !! 为什么不是我要的256色 or 8 bpp??
各位大虾看能不能修改一下这代码 把问题给纠正了
谢谢
HBITMAP BitmapFromDIB (HANDLE hDIB, HPALETTE hPal)
{
LPBITMAPINFOHEADER lpbih;
HPALETTE hPalOld;
HDC hDC;
HBITMAP hBitmap;
if (!hDIB)
return NULL; lpbih = (LPBITMAPINFOHEADER)GlobalLock(hDIB); if (!lpbih)
return NULL; hDC = GetDC(NULL); if (hPal){
hPalOld = SelectPalette(hDC, hPal, FALSE);
RealizePalette(hDC);
}
hBitmap = CreateDIBitmap(hDC,
lpbih,
CBM_INIT,
(LPSTR)lpbih + lpbih->biSize + ColorTableSize(lpbih),
(LPBITMAPINFO)lpbih,
DIB_RGB_COLORS ); if (hPal)
SelectPalette(hDC, hPalOld, FALSE); ReleaseDC(NULL, hDC);
GlobalUnlock(hDIB);
return hBitmap;
}
解决方案 »
- 在窗口onpaint之前作画
- LUA解析器 如何执行两个LUA脚本??怎么终止其中一个脚本?
- 建立带ADO 的DLL
- 新手求助:VC的第三方控件哪几个比较好?
- 为什么我在以unicode方式编译的时候会出现这个错误,LINK : fatal error LNK1104: cannot open file "mfc42ud.lib",怎么办啊,请大家指教!!
- 网络通信中基础问题
- vc ado新增字段为汉字数据表为汉字出错,改成英文就正确了
- vc1.52版的安装程序哪里有下载?
- 怎么改变VC显示的默认字体???同一个程序,在不同的机子上显示的字体不一样,有的字体歪歪斜斜的,怎么改变啊???
- 网上哪里有关于vc网络编程的书下载?
- 为什么我的m_deit.setsel(0,-1)实现不了,为什么?
- 关于数组定义的问题
是取系统默认的HDC句柄 !
if (!hPal)
{
CPalette DibPal;
CreateDIBPalette(hDib,&DibPal);
hPal = (HPALETTE) DibPal.Detach();
}
hOldPal = ::SelectPalette(hDC, hPal, FALSE);
::RealizePalette(hDC);其中CreateDIBPalette()函数如下:
BOOL CreateDIBPalette(HDIB hDIB, CPalette* pPal)
{
LPLOGPALETTE lpPal; // pointer to a logical palette
HANDLE hLogPal; // handle to a logical palette
HPALETTE hPal = NULL; // handle to a palette
int i; // loop index
WORD wNumColors; // number of colors in color table
LPSTR lpbi; // pointer to packed-DIB
LPBITMAPINFO lpbmi; // pointer to BITMAPINFO structure (Win3.0)
LPBITMAPCOREINFO lpbmc; // pointer to BITMAPCOREINFO structure (old)
BOOL bWinStyleDIB; // flag which signifies whether this is a Win3.0 DIB
BOOL bResult = FALSE;
/* if handle to DIB is invalid, return FALSE */
if (hDIB == NULL)
return FALSE;
lpbi = (LPSTR) ::GlobalLock((HGLOBAL) hDIB);
/* get pointer to BITMAPINFO (Win 3.0) */
lpbmi = (LPBITMAPINFO)lpbi;
/* get pointer to BITMAPCOREINFO (old 1.x) */
lpbmc = (LPBITMAPCOREINFO)lpbi;
/* get the number of colors in the DIB */
wNumColors = ::DIBNumColors(lpbi);
if (wNumColors != 0)
{
/* allocate memory block for logical palette */
hLogPal = ::GlobalAlloc(GHND, sizeof(LOGPALETTE)
+ sizeof(PALETTEENTRY)
* wNumColors);
/* if not enough memory, clean up and return NULL */
if (hLogPal == 0)
{
::GlobalUnlock((HGLOBAL) hDIB);
return FALSE;
}
lpPal = (LPLOGPALETTE) ::GlobalLock((HGLOBAL) hLogPal);
/* set version and number of palette entries */
lpPal->palVersion = PALVERSION;
lpPal->palNumEntries = (WORD)wNumColors;
/* is this a Win 3.0 DIB? */
bWinStyleDIB = IS_WIN30_DIB(lpbi);
for (i = 0; i < (int)wNumColors; i++)
{
if (bWinStyleDIB)
{
lpPal->palPalEntry[i].peRed = lpbmi->bmiColors[i].rgbRed;
lpPal->palPalEntry[i].peGreen = lpbmi->bmiColors[i].rgbGreen;
lpPal->palPalEntry[i].peBlue = lpbmi->bmiColors[i].rgbBlue;
lpPal->palPalEntry[i].peFlags = 0;
}
else
{
lpPal->palPalEntry[i].peRed = lpbmc->bmciColors[i].rgbtRed;
lpPal->palPalEntry[i].peGreen = lpbmc->bmciColors[i].rgbtGreen;
lpPal->palPalEntry[i].peBlue = lpbmc->bmciColors[i].rgbtBlue;
lpPal->palPalEntry[i].peFlags = 0;
}
}
/* create the palette and get handle to it */
bResult = pPal->CreatePalette(lpPal);
::GlobalUnlock((HGLOBAL) hLogPal);
::GlobalFree((HGLOBAL) hLogPal);
}
else
{
CWindowDC dcScreen(NULL);
if ( dcScreen.GetDeviceCaps(RASTERCAPS) & RC_PALETTE )
{
/* create the palette and get handle to it */
bResult = pPal->CreateHalftonePalette(&dcScreen);
}
else
{
pPal->DeleteObject();
bResult = TRUE;
}
}
::GlobalUnlock((HGLOBAL) hDIB);
return bResult;
}
HBITMAP CDib::CreateSection(CDC* pDC /* = NULL */)
{
if(m_lpBMIH == NULL) return NULL;
if(m_lpImage != NULL) return NULL; // can only do this if image doesn't exist
m_hBitmap = ::CreateDIBSection(pDC->GetSafeHdc(), (LPBITMAPINFO) m_lpBMIH,
DIB_RGB_COLORS, (LPVOID*) &m_lpImage, NULL, 0);
ASSERT(m_lpImage != NULL);
return m_hBitmap;
}
hPalOld = SelectPalette(hDC, hPal, FALSE);
RealizePalette(hDC);
} 我的代码里已经有这个了啊 hPal就是用CreateDIBPalette 建立的