You can create a palette of the DIB section, 8 bits per pixel and 256 colors and initialize the palette to shades of gray {0, 0, 0}, {1, 1, 1}, ... {255, 255, 255}.
The single GDI BitBlt
in this bitmap will be the gray source image. Here is a snippet of code (in C ++, ATL and WTL - but you should get an idea).
CWindowDC DesktopDc(NULL); CDC BitmapDc; ATLVERIFY(BitmapDc.CreateCompatibleDC(DesktopDc)); CBitmap Bitmap; CTempBuffer<BITMAPINFO> pBitmapInfo; const SIZE_T nBitmapInfoSize = sizeof (BITMAPINFO) + 256 * sizeof (RGBQUAD); pBitmapInfo.AllocateBytes(nBitmapInfoSize); ZeroMemory(pBitmapInfo, nBitmapInfoSize); pBitmapInfo->bmiHeader.biSize = sizeof pBitmapInfo->bmiHeader; pBitmapInfo->bmiHeader.biWidth = 320; pBitmapInfo->bmiHeader.biHeight = 240; pBitmapInfo->bmiHeader.biPlanes = 1; pBitmapInfo->bmiHeader.biBitCount = 8; pBitmapInfo->bmiHeader.biCompression = BI_RGB; pBitmapInfo->bmiHeader.biSizeImage = 240 * 320; pBitmapInfo->bmiHeader.biClrUsed = 256; pBitmapInfo->bmiHeader.biClrImportant = 256; for(SIZE_T nIndex = 0; nIndex < 256; nIndex++) { pBitmapInfo->bmiColors[nIndex].rgbRed = (BYTE) nIndex; pBitmapInfo->bmiColors[nIndex].rgbGreen = (BYTE) nIndex; pBitmapInfo->bmiColors[nIndex].rgbBlue = (BYTE) nIndex; } Bitmap.Attach(CreateDIBSection(DesktopDc, pBitmapInfo, 0, DIB_RGB_COLORS, NULL, 0)); ATLVERIFY(Bitmap); BitmapDc.SelectBitmap(Bitmap); //////////////////////////////////////////////// // This is what greys it out: ATLVERIFY(BitBlt(BitmapDc, 0, 0, 320, 240, DesktopDc, 0, 0, SRCCOPY)); //////////////////////////////////////////////// ATLVERIFY(BitBlt(DesktopDc, 0, 240, 320, 240, BitmapDc, 0, 0, SRCCOPY));
Roman R.
source share