32 Bitmaps -> 24 Dibs -> .bmp File
-
Grrr... I have done so much bitmap work that I feel embarassed even asking this! :) But I'm stuck! I create a ddb on a 32 bits per pixel display. Now I want to save this as a 24 bit bitmap --- rather than a 32 bit bitmap (which I can do just fine). I'm using the dib api functions provided by microsoft (slighty modified), namely this function:
HDIB WINAPI CreateDibFromBitmap(HBITMAP hBitmap, HPALETTE hPal, int bitsPerPixel) { HDIB hDIB = NULL; BITMAP bm; // bitmap structure BITMAPINFOHEADER bi; // bitmap header BITMAPINFOHEADER FAR *lpbi = NULL; // pointer to BITMAPINFOHEADER DWORD dwLen = 0; // size of memory block HDIB h = NULL; // handle to DIB, temp handle HDC hDC = NULL; // handle to DC WORD biBits = 0; // bits per pixel /* check if bitmap handle is valid */ if (!hBitmap) return NULL; /* fill in BITMAP structure, return NULL if it didn't work */ if (!GetObject(hBitmap, sizeof(bm), (LPSTR)&bm)) return NULL; /* if no palette is specified, use default palette */ if (hPal == NULL) hPal = (HPALETTE)::GetStockObject(DEFAULT_PALETTE); if(bitsPerPixel == 0) { /* calculate bits per pixel */ biBits = bm.bmPlanes * bm.bmBitsPixel; } else { // Use specified bits per pixel. biBits = bitsPerPixel; } /* make sure bits per pixel is valid */ if (biBits <= 1) biBits = 1; else if (biBits <= 4) biBits = 4; else if (biBits <= 8) biBits = 8; else /* if greater than 8-bit, force to 24-bit */ biBits = 24; /* initialize BITMAPINFOHEADER */ bi.biSize = sizeof(BITMAPINFOHEADER); bi.biWidth = bm.bmWidth; bi.biHeight = bm.bmHeight; bi.biPlanes = 1; bi.biBitCount = biBits; bi.biCompression = BI_RGB; bi.biSizeImage = 0; bi.biXPelsPerMeter = 0; bi.biYPelsPerMeter = 0; bi.biClrUsed = 0; bi.biClrImportant = 0; /* calculate size of memory block required to store BITMAPINFO */ dwLen = bi.biSize + PaletteSize((LPSTR)&bi); /* get a DC */ hDC = GetDC(NULL); /* select and realize our palette */ hPal = SelectPalette(hDC, hPal, FALSE); RealizePalette(hDC); /* alloc memory block to store our bitmap */ hDIB = (HDIB)GlobalAlloc(GHND, dwLen); /* if we couldn't get memory block */ if (!hDIB) { /* clean up and return NULL */ SelectPalett