你可以不必將與設(shè)備無關(guān)(DIB)位圖轉(zhuǎn)換為與設(shè)備相關(guān)(DDB)位圖,也能
將它顯示在設(shè)備上。然而,當使用DIB時處理過程很慢,并且你不能使用一些函
數(shù),如BitBlt,以為它只適用于DDB位圖。
有關(guān)從DIB位圖轉(zhuǎn)換為DDB位圖有下面幾步:
1、根據(jù)DIB色表信息建立邏輯調(diào)色盤,只是在如果設(shè)備支持調(diào)色盤時需要這
樣做。使用DIB色表中的信息初始化結(jié)構(gòu)LOGPALETTE,使用CreatePalette()函
數(shù)建立調(diào)色盤。
2、將邏輯調(diào)色盤選入想要建立DDB的DC中,并RealizePalette()。
3、使用CreateDIBBitmap()函數(shù)建立DDB。
4、不要忘記釋放相關(guān)資源。
HBITMAP DIBToDDB( HANDLE hDIB )
{
LPBITMAPINFOHEADER lpbi;
HBITMAP hbm;
CPalette pal;
CPalette* pOldPal;
CClientDC dc(NULL);
if (hDIB == NULL)
return NULL;
lpbi = (LPBITMAPINFOHEADER)hDIB;
int nColors = lpbi->biClrUsed ? lpbi->biClrUsed :
1 << lpbi->biBitCount;
BITMAPINFO &bmInfo = *(LPBITMAPINFO)hDIB ;
LPVOID lpDIBBits;
if( bmInfo.bmiHeader.biBitCount > 8 )
lpDIBBits = (LPVOID)((LPDWORD)(bmInfo.bmiColors +
bmInfo.bmiHeader.biClrUsed) +
((bmInfo.bmiHeader.biCompression == BI_BITFIELDS) ? 3 : 0));
else
lpDIBBits = (LPVOID)(bmInfo.bmiColors + nColors);
// Create and select a logical palette if needed
if( nColors <= 256 && dc.GetDeviceCaps(RASTERCAPS) & RC_PALETTE)
{
UINT nSize = sizeof(LOGPALETTE) + (sizeof(PALETTEENTRY) * nColors);
LOGPALETTE *pLP = (LOGPALETTE *) new BYTE[nSize];
pLP->palVersion = 0x300;
pLP->palNumEntries = nColors;
for( int i=0; i < nColors; i++)
{
pLP->palPalEntry[i].peRed = bmInfo.bmiColors[i].rgbRed;
pLP->palPalEntry[i].peGreen = bmInfo.bmiColors[i].rgbGreen;
pLP->palPalEntry[i].peBlue = bmInfo.bmiColors[i].rgbBlue;
pLP->palPalEntry[i].peFlags = 0;
}
pal.CreatePalette( pLP );
delete[] pLP;
// Select and realize the palette
pOldPal = dc.SelectPalette( &pal, FALSE );
dc.RealizePalette();
}
hbm = CreateDIBitmap(dc.GetSafeHdc(), // handle to device context
(LPBITMAPINFOHEADER)lpbi, // pointer to bitmap info header
(LONG)CBM_INIT, // initialization flag
lpDIBBits, // pointer to initialization data
(LPBITMAPINFO)lpbi, // pointer to bitmap info
DIB_RGB_COLORS ); // color-data usage
if (pal.GetSafeHandle())
dc.SelectPalette(pOldPal,FALSE);
return hbm;
}
和上面重復的,但是結(jié)構(gòu)更清晰
你可以不必將與設(shè)備無關(guān)(DIB)位圖轉(zhuǎn)換為與設(shè)備相關(guān)(DDB)位圖,也能將它顯示在設(shè)備上。然而,當使用DIB時處理過程很慢,并且你不能使用一些函數(shù),如BitBlt,以為它只適用于DDB位圖。有關(guān)從DIB位圖轉(zhuǎn)換為DDB位圖有下面幾步:1、根據(jù)DIB色表信息建立邏輯調(diào)色盤,只是在如果設(shè)備支持調(diào)色盤時需要這樣做。使用DIB色表中的信息初始化結(jié)構(gòu)LOGPALETTE,使用CreatePalette()函數(shù)建立調(diào)色盤。2、將邏輯調(diào)色盤選入想要建立DDB的DC中,并RealizePalette()。3、使用CreateDIBBitmap()函數(shù)建立DDB。4、不要忘記釋放相關(guān)資源。HBITMAP DIBToDDB( HANDLE hDIB ){LPBITMAPINFOHEADER lpbi;HBITMAP hbm;CPalette pal;CPalette* pOldPal;CClientDC dc(NULL);if (hDIB == NULL)return NULL;lpbi = (LPBITMAPINFOHEADER)hDIB;int nColors = lpbi->biClrUsed ? lpbi->biClrUsed :1 << lpbi->biBitCount;BITMAPINFO &bmInfo = *(LPBITMAPINFO)hDIB ;LPVOID lpDIBBits;if( bmInfo.bmiHeader.biBitCount > 8 )lpDIBBits = (LPVOID)((LPDWORD)(bmInfo.bmiColors +bmInfo.bmiHeader.biClrUsed) +((bmInfo.bmiHeader.biCompression == BI_BITFIELDS) ? 3 : 0));elselpDIBBits = (LPVOID)(bmInfo.bmiColors + nColors);// Create and select a logical palette if neededif( nColors <= 256 && dc.GetDeviceCaps(RASTERCAPS) & RC_PALETTE){UINT nSize = sizeof(LOGPALETTE) + (sizeof(PALETTEENTRY) * nColors);LOGPALETTE *pLP = (LOGPALETTE *) new BYTE[nSize];pLP->palVersion = 0x300;pLP->palNumEntries = nColors;for( int i=0; i < nColors; i++){pLP->palPalEntry[i].peRed = bmInfo.bmiColors[i].rgbRed;pLP->palPalEntry[i].peGreen = bmInfo.bmiColors[i].rgbGreen;pLP->palPalEntry[i].peBlue = bmInfo.bmiColors[i].rgbBlue;pLP->palPalEntry[i].peFlags = 0;}pal.CreatePalette( pLP );delete[] pLP;// Select and realize the palettepOldPal = dc.SelectPalette( &pal, FALSE );dc.RealizePalette();}hbm = CreateDIBitmap(dc.GetSafeHdc(), // handle to device context(LPBITMAPINFOHEADER)lpbi, // pointer to bitmap info header(LONG)CBM_INIT, // initialization flaglpDIBBits, // pointer to initialization data(LPBITMAPINFO)lpbi, // pointer to bitmap infoDIB_RGB_COLORS ); // color-data usageif (pal.GetSafeHandle())dc.SelectPalette(pOldPal,FALSE);return hbm;}
本站僅提供存儲服務,所有內(nèi)容均由用戶發(fā)布,如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請
點擊舉報。