LPBITMAPINFOHEADER DDBToDIB(HBITMAP hbitmap, HPALETTE hPal)
{
TRACE ("DDBToDIB 1 \n");
BITMAP bm;
BITMAPINFOHEADER bi;
LPBITMAPINFOHEADER lpbi, lpbi2;
DWORD dwLen;
HDC hDC;
HPALETTE hPal2;
BOOL bGotBits;
int nColors;
// If a palette has not been supplied, use default palette
if (hPal==NULL)
hPal = (HPALETTE) GetStockObject(DEFAULT_PALETTE);
// Get bitmap information
GetObject (hbitmap, sizeof(bm), (LPSTR)&bm);
// Initialize the bitmap infoheader
bi.biSize = sizeof(BITMAPINFOHEADER);
bi.biWidth = bm.bmWidth;
bi.biHeight = bm.bmHeight;
bi.biPlanes = 1;
bi.biBitCount = bm.bmPlanes * bm.bmBitsPixel;
//bm.bmPlanes * bm.bmBitsPixel;
bi.biCompression = BI_RGB;
bi.biSizeImage = 0;
bi.biXPelsPerMeter = 0;
bi.biYPelsPerMeter = 0;
bi.biClrUsed = 0;
bi.biClrImportant = 0;
// Compute the size of the infoheader and the color table
nColors = (1 << bi.biBitCount);
if( nColors > 256 )
nColors = 0;
dwLen = bi.biSize + nColors * sizeof(RGBQUAD);
// We need a device context to get the DIB from
hDC = GetDC(NULL);
hPal2 = SelectPalette(hDC,hPal,FALSE);
RealizePalette (hDC);
// Allocate enough memory to hold bitmap infoheader and
// color table
lpbi = (LPBITMAPINFOHEADER) GlobalAlloc (GMEM_FIXED,dwLen);
if (!lpbi){
SelectPalette (hDC,hPal,FALSE);
ReleaseDC (NULL,hDC);
return NULL;
}
*lpbi = bi;
// Call GetDIBits with a NULL lpBits param, so the device
// driver will calculate the biSizeImage field
GetDIBits(hDC, hbitmap, 0L,
(DWORD)bi.biHeight,
(LPBYTE)NULL, (LPBITMAPINFO)lpbi,
(DWORD)DIB_RGB_COLORS);
bi = *lpbi;
// If the driver did not fill in the biSizeImage field, then
// compute it
// Each scan line of the image is aligned on a DWORD (32bit)
// boundary
if (bi.biSizeImage == 0){
bi.biSizeImage = ((((bi.biWidth * bi.biBitCount) + 31)
& ~31) / 8) * bi.biHeight;
}
// Realloc the buffer so that it can hold all the bits
dwLen += bi.biSizeImage;
if (lpbi2 = (LPBITMAPINFOHEADER) GlobalReAlloc (lpbi, dwLen, GMEM_MOVEABLE))
lpbi2 = lpbi;
else{
GlobalFree(lpbi);
// Reselect the original palette
SelectPalette (hDC,hPal,FALSE);
ReleaseDC(NULL,hDC);
TRACE ("DDBToDIB 2 \n");
int fehler=GetLastError();
return NULL;
TRACE ("DDBToDIB 3 \n");
}
// FINALLY get the DIB
bGotBits = GetDIBits( hDC, hbitmap,
0L, // Start scan line
(DWORD)bi.biHeight, // # of scan lines
(LPBYTE)lpbi // address for bitmap bits
+ (bi.biSize + nColors * sizeof(RGBQUAD)),
(LPBITMAPINFO)lpbi, // address of bitmapinfo
(DWORD)DIB_RGB_COLORS); // Use RGB for color table
SelectPalette(hDC,hPal,FALSE);
ReleaseDC(NULL,hDC);
if( !bGotBits )
{
GlobalFree(lpbi);
return NULL;
}
DeleteObject (hPal);
return lpbi;
}
BOOL BMPtoFILE(LPBITMAPINFOHEADER lpbi)
{
FILE *Datei;
BITMAPFILEHEADER hdr;
int nColors;
char Dateiname [100];
static short Dateinummer;
BOOL gibts;
do // Dateinamen bestimmen
{
gibts = FALSE;
sprintf (Dateiname, "CAP%04d.bmp", Dateinummer);
// existiert schon
Datei = fopen (Dateiname, "r");
if (Datei)
{
gibts = TRUE;
fclose (Datei);
}
Dateinummer ++;
}
while (gibts);
Datei = fopen (Dateiname, "wb"); // write, binaer...
nColors= 1 << lpbi->biBitCount;
hdr.bfType= ((WORD) ('M' << 8) | 'B');
hdr.bfSize=sizeof(hdr) + GlobalSize(lpbi);
hdr.bfReserved1=0;
hdr.bfReserved2=0;
hdr.bfOffBits=(DWORD) (sizeof(hdr) + nColors * sizeof(RGBQUAD) + lpbi->biSize);
fwrite (&hdr,sizeof(hdr), 1, Datei);
fwrite (lpbi,GlobalSize(lpbi), 1, Datei);
fclose (Datei);
return (TRUE);
}
BOOL SaveWin(HWND hwnd)
{
//it will capture a wnd image and save it into a bmp file
TRACE ("SaveWin 1 \n");
HBITMAP hbmp,hOldBmp;
HDC hdc;
RECT rect;
BOOL flg=0;
HPALETTE hpal;
LOGPALETTE *pLp;
HDC hmemdc;
LPBITMAPINFOHEADER lpbi;
hdc = GetDC (hwnd); // Device Context aus Fenster
GetClientRect (hwnd, &rect); //Get dimension of Window
hmemdc = CreateCompatibleDC (hdc); //Make Compatible DC for memdc
hbmp = CreateCompatibleBitmap(hdc, rect.right, rect.bottom);
//Create Compatible DDB
hOldBmp = (HBITMAP) SelectObject(hmemdc, hbmp);
BitBlt (hmemdc, 0,0,rect.right, rect.bottom, hdc, 0, 0, SRCCOPY);
//The following code will detect whether the BMP uses a Raster
//palette or not.
if(GetDeviceCaps(hdc, RASTERCAPS) & RC_PALETTE)
{
int nSize;
nSize=sizeof(LOGPALETTE) + sizeof(PALETTEENTRY) * 256;
pLp = (LOGPALETTE*) malloc (nSize);
pLp->palVersion=0x300;
pLp->palNumEntries=GetSystemPaletteEntries(
hdc,0,255,pLp->palPalEntry);
hpal = CreatePalette(pLp);
}
SelectObject(hmemdc, hOldBmp);
// convert bitmap from DDB to DIB see DDBToDIB()
// See DDBToDIB function for more..
lpbi = DDBToDIB (hbmp, hpal);
if (lpbi==NULL)
TRACE ("SaveWin 2 \n");
return FALSE;
TRACE ("SaveWin 3 \n");
BMPtoFILE (lpbi); // und ab damit...!
ReleaseDC (hwnd, hdc);
DeleteDC (hmemdc);
DeleteObject (hpal);
DeleteObject (hbmp);
//free (pLp);
return (TRUE);
}
HWND hwnd;
void CBitmapDoc::OnButtonStore()
{
// TODO: Code für Befehlsbehandlungsroutine hier einfügen
TRACE ("OnButtonStore 1 \n");
SaveWin(hwnd);
TRACE ("OnButtonStore 2 \n");
}