009
07.02.2006, 20:54 Uhr
~berny
Gast
|
habe noch was gefunden, wie Du mit bordeigenen Mitteln das Fenster als bmp abspeichern kannst. Trotzdem empfehle ich die Cximage Methode, denn wenn einmal eingebunden kannst Du z.B. ohne große Probleme vergrößern, drehen....etc.
void SaveBitmap() { CDC dcScaled; CBitmap bmScaled; BITMAP bmp; PBITMAPINFO pbmi; PBITMAPINFOHEADER pbih; BITMAPFILEHEADER bfh; WORD cClrBits; LPBYTE lpBits; DWORD cb; BYTE *hp; unsigned long NrBmpBits; CRect r; HWND hDeskTop = ::GetActiveWindow( ); CDC* pDC= CWnd::FromHandle(hDeskTop) ->GetWindowDC(); GetClientRect(r); dcScaled.CreateCompatibleDC(pDC); int nWidth = abs(r.right - r.left); int nHeight = abs(r.bottom - r.top); if( bmScaled.CreateCompatibleBitmap( pDC,nWidth,nHeight ) == 0 ) { AfxMessageBox("Could not Create compatible Bitmap"); return; } if(!dcScaled.SelectObject(bmScaled)) { AfxMessageBox("Could not select Bitmap-Object"); return; } if(!dcScaled.BitBlt(0,0,nWidth,nHeight,pDC,0,0,SRCCOPY)) { AfxMessageBox("Could not copy Bitmap-Object"); return; } if(!bmScaled.GetObject(sizeof(BITMAP),&bmp)) { AfxMessageBox("Could not get Bitmap-Info"); return; }
cClrBits = (WORD)(bmp.bmPlanes * bmp.bmBitsPixel); if(cClrBits == 1) cClrBits = 1; else if(cClrBits <=4) cClrBits = 4; else if(cClrBits <=8) cClrBits = 8; else if(cClrBits <=16) cClrBits = 16; else if(cClrBits <=24) cClrBits = 24; else cClrBits = 32;
if( cClrBits != 24 ) pbmi = (PBITMAPINFO)LocalAlloc(LPTR,sizeof(BITMAPINFOHEADER) + sizeof(RGBQUAD) * (1 << cClrBits)); else pbmi = (PBITMAPINFO)LocalAlloc(LPTR,sizeof(BITMAPINFOHEADER));
memset(pbmi,0,sizeof(BITMAPINFOHEADER)); pbmi->bmiHeader.biSize = sizeof(BITMAPINFOHEADER); pbmi->bmiHeader.biWidth = bmp.bmWidth; pbmi->bmiHeader.biHeight = bmp.bmHeight; pbmi->bmiHeader.biPlanes = bmp.bmPlanes; pbmi->bmiHeader.biBitCount = bmp.bmBitsPixel; if(cClrBits < 24) pbmi->bmiHeader.biClrUsed = (1 << cClrBits); pbmi->bmiHeader.biCompression = BI_RGB; NrBmpBits = ((pbmi->bmiHeader.biWidth * cClrBits +31) & ~31) /8 * pbmi->bmiHeader.biHeight; pbmi->bmiHeader.biSizeImage = NrBmpBits; pbmi->bmiHeader.biClrImportant = 0; pbih = (PBITMAPINFOHEADER) pbmi; lpBits = (LPBYTE) GlobalAlloc(GMEM_FIXED,NrBmpBits); if( !lpBits ) { AfxMessageBox("GlobalAlloc in SaveBitmap failed"); return; } if( !GetDIBits(dcScaled,bmScaled,0, (WORD)pbih->biHeight,lpBits,pbmi,DIB_RGB_COLORS)) { AfxMessageBox("GetDIBits in SaveBitmap failed"); return; }
HANDLE hf = CreateFile( "Test.bmp", GENERIC_WRITE, FILE_SHARE_READ, NULL, CREATE_ALWAYS, NULL, NULL );
if( hf == INVALID_HANDLE_VALUE ) { AfxMessageBox("CreateFile in SaveBitmap failed"); return; }
// write out the file header // memset( &bfh, 0, sizeof( bfh ) ); bfh.bfType = 'MB'; bfh.bfSize = (DWORD)( sizeof( BITMAPFILEHEADER ) + pbih->biSize + pbih->biClrUsed * sizeof(RGBQUAD) + pbih->biSizeImage); bfh.bfReserved1 = 0; bfh.bfReserved2 = 0; bfh.bfOffBits = (DWORD)(sizeof( BITMAPFILEHEADER ) + pbih->biSize + pbih->biClrUsed * sizeof(RGBQUAD));
DWORD dwWritten = 0; if( !WriteFile( hf, (LPVOID)&bfh, sizeof( BITMAPFILEHEADER ), &dwWritten, NULL )) { AfxMessageBox("Write Bitmapfileheader in SaveBitmap failed"); return; }
// and the bitmap format // dwWritten = 0; if( !WriteFile( hf, (LPVOID)pbih, sizeof( BITMAPINFOHEADER ) + pbih->biClrUsed * sizeof(RGBQUAD), &dwWritten, NULL )) { AfxMessageBox("Write Bitmapinfoheader in SaveBitmap failed"); return; }
// and the bits themselves // cb = NrBmpBits; hp = lpBits; dwWritten = 0; if( !WriteFile( hf, (LPSTR)hp, cb, &dwWritten, NULL )) { AfxMessageBox("Write Bitmapdata in SaveBitmap failed"); return; } CloseHandle( hf ); LocalFree((HLOCAL)pbmi); GlobalFree((HGLOBAL)lpBits); DeleteObject(bmScaled); } |