I am doing the following steps in order to display the coloured bitmap against the selected Logical Palette. (this is what I exactly need ).
- Initialise CBitmap object ( CreateBitmapIndirect() )
- Select bitmap object against source DC
- Select logical palette against destination DC
- Realize logical palette against destination DC
- BitBlt
Problem is, Bitmap is drawn against default palette, instead against the
selected Logical Palette.
Pallavi
May 31st, 1999, 05:00 AM
We have done it like this, and it works.
This peice of code was picked up from somewhere in codeguru itself.
// Override OnPaint of the class
void ClassName::OnPaint()
{
CPaintDC dc(this); // device context for painting
CDC memDC;
memDC.CreateCompatibleDC( &dc );
CBitmap bitmap;
CPalette palette;
GetBitmapAndPalette( IDB_BitmapID, bitmap, palette );
memDC.SelectObject( &bitmap ); // Select and realize the palette
if( dc.GetDeviceCaps(RASTERCAPS) & RC_PALETTE && palette.m_hObject != NULL )
{
dc.SelectPalette( &palette, FALSE );
dc.RealizePalette();
}
dc.BitBlt(0, 0, 180, 430, &memDC, 0, 0,SRCCOPY);
}
BOOL ClassName::GetBitmapAndPalette(UINT nIDResource, CBitmap &bitmap, CPalette &pal)
{
LPCTSTR lpszResourceName = (LPCTSTR)nIDResource;
HBITMAP hBmp = (HBITMAP)::LoadImage( AfxGetInstanceHandle(), lpszResourceName, IMAGE_BITMAP, 0,0, LR_CREATEDIBSECTION );
if( hBmp == NULL )
return FALSE;
bitmap.Attach( hBmp );
// Create a logical palette for the bitmap
DIBSECTION ds;
BITMAPINFOHEADER &bmInfo = ds.dsBmih;
bitmap.GetObject( sizeof(ds), &ds );
int nColors = bmInfo.biClrUsed ? bmInfo.biClrUsed : 1 << bmInfo.biBitCount;
// Create a halftone palette if colors > 256.
CClientDC dc(NULL);// Desktop DC
if( nColors > 256 )
pal.CreateHalftonePalette( &dc );
else
{
// Create the palette
RGBQUAD *pRGB = new RGBQUAD[nColors];
CDC memDC;
memDC.CreateCompatibleDC(&dc);
memDC.SelectObject( &bitmap );
::GetDIBColorTable( memDC, 0, nColors, pRGB );
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 = pRGB[i].rgbRed;
pLP->palPalEntry[i].peGreen = pRGB[i].rgbGreen;
pLP->palPalEntry[i].peBlue = pRGB[i].rgbBlue;
pLP->palPalEntry[i].peFlags = 0;
}
pal.CreatePalette( pLP );
delete[] pLP;
delete[] pRGB;
}
return TRUE;
}
Hope this helps.