R
Hi
hab vor ziemlich langer Zeit mal ne Funktion geschrieben.
Darin kannst du sehen, wie sowas funktioniert.
Du musst sie aber ein bisschen abwandeln:
CBitmap* MakeScreenShot(CWnd* pWnd)
{
if(pWnd == NULL) return NULL;
// Das Rechteck, was gezeichnet wird (x = 230, y = 10, Länge x = 8, Länge y = 12):
int nX = 230;
int nY = 10;
int nWidth = 8;
int nHeight = 12;
CDC* pSource = pWnd->GetDC(); // das DC des Fensters
CDC* pMemDC = new CDC; // das DC, das mit dem Bitmap verknüpft wird
pMemDC->CreateCompatibleDC(pSource); // beide DCs gleich
CBitmap* pBitmap = new CBitmap;
pBitmap->CreateCompatibleBitmap(pSource, nWidth, nHeight);
pBitmap->SetBitmapDimension(nWidth, nHeight); // damit GetBitmapDimension funktioniert
pMemDC->SelectObject(pBitmap); // pMemDC mit Bitmap verknüpfen
if(! pMemDC->BitBlt(0, 0, nWidth, nHeight, pSource, nX, nY, SRCCOPY))
{
pMemDC->DeleteDC();
delete pMemDC;
pApp->ReleaseDC(pSource);
}
pMemDC->DeleteDC();
delete pMemDC;
pWnd->ReleaseDC(pSource);
return pBitmap; // Bitmap muss nachher UNBEDINGT gelöscht werden
}
Speichern kannst du das im bmp Format folgendermaßen:
BOOL WriteBitmap( LPTSTR szFile, HBITMAP hbitmap, HDC memdc)
{
BITMAP bmp;
if(GetObject(hbitmap, sizeof(BITMAP), &bmp))
{
BITMAPINFOHEADER BmpInfoHdr; //Struktur für Bitmap-Infoheader
BITMAPFILEHEADER BmpFileHdr; //Struktur für Bitmap-Dateiheader
BmpInfoHdr.biSize = sizeof(BITMAPINFOHEADER);
BmpInfoHdr.biWidth = bmp.bmWidth;
BmpInfoHdr.biHeight = bmp.bmHeight;
BmpInfoHdr.biPlanes = bmp.bmPlanes;
BmpInfoHdr.biBitCount = 24;
BmpInfoHdr.biCompression = BI_RGB;
BmpInfoHdr.biSizeImage = bmp.bmWidth*bmp.bmHeight*3;
BmpFileHdr.bfType = 0x4d42;
BmpFileHdr.bfReserved1 = 0;
BmpFileHdr.bfReserved2 = 0;
BmpFileHdr.bfOffBits = sizeof(BITMAPFILEHEADER)+sizeof(BITMAPINFOHEADER);
BmpFileHdr.bfSize = BmpFileHdr.bfOffBits+BmpInfoHdr.biSizeImage;
bmp.bmBits = (void*)GlobalAlloc(GMEM_FIXED, BmpInfoHdr.biSizeImage);
if(GetDIBits(memdc, hbitmap, 0, BmpInfoHdr.biHeight, bmp.bmBits,
(BITMAPINFO*)&BmpInfoHdr, DIB_RGB_COLORS) == BmpInfoHdr.biHeight)
{
HANDLE hFile = CreateFile(szFile, GENERIC_READ | GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL,NULL);
if(hFile != INVALID_HANDLE_VALUE) {
DWORD dwTmp;
WriteFile(hFile, &BmpFileHdr, sizeof(BITMAPFILEHEADER), &dwTmp, NULL);
WriteFile(hFile, &BmpInfoHdr, sizeof(BITMAPINFOHEADER), &dwTmp, NULL);
WriteFile(hFile, bmp.bmBits, BmpInfoHdr.biSizeImage, &dwTmp, NULL);
}
CloseHandle(hFile);
}
GlobalFree(bmp.bmBits);
return TRUE;
}
return FALSE;
}
Wenn du das Bitmap in einem anderen Format speichern willst, brauchst du eine Klasse, die das kann. Bei Codeguru und Codeprojekt gibts duzente
Grüße Rapha