Hi all:
I have a piece of code which captures the screen and saves it as a bitmap file. When i use 32 and 24 bit color it works fine, however..when i set the display to 256 colors, it gives me a blank screen...why?????
Appriciate the help.
Thanks,
Preetham.
Almost certainly related to palettes. 32 and 24-bit desktops are easily captured because the code doesn't need to worry about palettes. 256 colors and less can, oddly, be a little more complex
The method GetScreenBitmap() is used to capture the screen.
There are some redundant operations performed in the method, which u can ignore or modify....
typedef struct
{
int indexOld ; // The original palette index
int indexNew ; // The new palette index
DWORD dwCount ; // The number of pixels of that color
RGBQUAD rgb ; // The RGB value for that index
}
COLORCOUNT ;
BYTE * DibSortColorTable (BYTE * pDib);
DWORD DibGetPixel (PBYTE pDib, int iRow, int iCol) ;
void DibSetPixel (PBYTE pDib, int iRow, int iCol, DWORD dwPixel) ;
//Get the Screen Bitmap and return a global handle to the memory containing the bmp
BOOL
GetScreenBitmap(HDC hdcscr)
{
HPALETTE hPalette;
BYTE * pdib;
RECT appRectNew;
BITMAPFILEHEADER bmfh ;
DWORD dwTotalSize, dwBitsSize ;
//DeleteObject(hPalette);
//set the buffer size and the Buffer pointer
memScreenDataSize = bmfh.bfSize;
screenInfoCard->bufSize = bmfh.bfSize;
screenInfoCard->hSize = appRect.right - appRect.left;
screenInfoCard->vSize = appRect.bottom - appRect.top;
prevMemScreenDataSize = bmfh.bfSize;
// Open the output file
//write the bmp into the memory
DWORD dwTemp =0;
HANDLE hf = CreateFile("testCaptureBitmap.bmp",
GENERIC_READ | GENERIC_WRITE,
(DWORD) 0,
NULL,
CREATE_ALWAYS,
FILE_ATTRIBUTE_NORMAL,
(HANDLE) NULL);
WriteFile(hf, memScreenData, bmfh.bfSize,
(LPDWORD) &dwTemp, NULL);
CloseHandle(hf);
free(pdib);
return true;
}
/*-------------------------------------------------------
GetCurrentPalette: Obtains the current system palette.
-------------------------------------------------------*/
// Now create a palette based on the system palette entries
hPalette = CreatePalette (plogpal) ;
// free the memory
free (plogpal) ;
}
// If the device doesn't support palettes, hPalette is NULL.
else
hPalette = NULL ;
// Release the DC
ReleaseDC (hwnd, hdc) ;
return hPalette ;
}
/*-------------------------------------------------------------------------
ConvertDDBtoDIB: Converts a GDI bitmap object to a DIB using a palette.
Function based on Knowledge Base article Q80080.
-------------------------------------------------------------------------*/
/*----------------------------------------------------------------------
DibSortColorTable: Sorts a DIB color table to put most-used colors
at the beginning. Possibly reduces size of DIB
for unused colors, so returns pointer to new DIB.
----------------------------------------------------------------------*/
BYTE * DibSortColorTable (BYTE * pDib)
{
COLORCOUNT cc[256] ;
int cRows, cCols, cClrs, iRow, iCol, i, cClrsNew ;
// This function is only applicable for palette DIBs
if (DibBitCount(pDib) > 8)
return pDib ;
// Get some useful information about the DIB using macros
// defined in ScreenSeizeDib.h
/*--------------------------------------------------------------------------
DibGetPixel: Gets a pixel value for a particular row and column address.
NOTE: No bounds check on row and column!
--------------------------------------------------------------------------*/
DWORD DibGetPixel (PBYTE pDib, int iRow, int iCol)
{
// Handy macro in ScreenSeizeDib.h
/*--------------------------------------------------------------------------
DibSetPixel: Sets a pixel value for a particular row and column address.
NOTE: No bounds check on row and column!
--------------------------------------------------------------------------*/
void DibSetPixel (PBYTE pDib, int iRow, int iCol, DWORD dwPixel)
{
// Ditto
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.