?
Moin Moin!!!
Ich habe eine Func, die mir einen Screenshot erstellt.
Leider ist das Bild, wenn ich es in einen Viewer öffne, schwarz.
Desweiteren habe ich den Verdacht, dass vorher initialisierter Speicher nicht wieder freigegeben wird. Könntet ihr bitte mal einen Blick auf die Funcs werfen? Danke.
BOOL MakeScreenshot(LPWSTR lpDestination, LPWSTR lpFileName)
{
WCHAR path[MAX_PATH + 1];
HDC hScreenDC, hBufferDC;
HBITMAP hBufferBitmap;
int nWidth, nHeight;
PBITMAPINFO pBufferBitmapInfo;
lstrcpy(path, lpDestination);
PathAppend(path, lpFileName);
hScreenDC = GetDC(0);
nWidth = GetDeviceCaps(hScreenDC, HORZRES);
nHeight = GetDeviceCaps(hScreenDC, VERTRES);
hBufferDC = CreateCompatibleDC(hScreenDC);
hBufferBitmap = CreateCompatibleBitmap(hBufferDC, nWidth, nHeight);
if (hBufferBitmap == NULL)
return FALSE;
if (SelectObject(hBufferDC, hBufferBitmap) == NULL)
return FALSE;
if (!BitBlt(hBufferDC, 0, 0, nWidth, nHeight, hScreenDC, 0, 0, SRCCOPY))
return FALSE;
pBufferBitmapInfo = CreateBitmapInfo(hBufferBitmap);
CreateBitmapFile(NULL, path, pBufferBitmapInfo, hBufferBitmap, hBufferDC);
}
PBITMAPINFO CreateBitmapInfo(HBITMAP hBmp)
{
BITMAP bmp;
PBITMAPINFO pbmi;
WORD cClrBits;
if (!GetObject(hBmp, sizeof(BITMAP), (LPSTR)&bmp))
return NULL;
// Convert the color format to a count of bits.
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));
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);
// If the bitmap is not compressed, set the BI_RGB flag.
pbmi->bmiHeader.biCompression = BI_RGB;
pbmi->bmiHeader.biSizeImage = ((pbmi->bmiHeader.biWidth * cClrBits +31) & ~31) /8
* pbmi->bmiHeader.biHeight;
pbmi->bmiHeader.biClrImportant = 0;
return pbmi;
}
BOOL CreateBitmapFile(HWND hWnd, LPWSTR lpFileName, PBITMAPINFO pbi, HBITMAP hBitmap, HDC hDC)
{
HANDLE hFile;
BITMAPFILEHEADER hdr;
PBITMAPINFOHEADER pbih;
LPBYTE lpBits;
DWORD dwTotal;
DWORD cb;
BYTE *hp;
DWORD dwTmp;
pbih = (PBITMAPINFOHEADER)pbi;
lpBits = (LPBYTE)GlobalAlloc(GMEM_FIXED, pbih->biSizeImage);
if (!lpBits)
return FALSE;
if (!GetDIBits(hDC, hBitmap, 0, (WORD)pbih->biHeight, lpBits, pbi, DIB_RGB_COLORS))
return FALSE;
hFile = CreateFile(lpFileName,
GENERIC_READ | GENERIC_WRITE,
0,
NULL,
CREATE_ALWAYS,
FILE_ATTRIBUTE_NORMAL,
NULL);
if (hFile == INVALID_HANDLE_VALUE)
return FALSE;
hdr.bfType = 0x4d42;
hdr.bfSize = (DWORD)(sizeof(BITMAPFILEHEADER) + pbih->biSize + pbih->biClrUsed * sizeof(RGBQUAD) + pbih->biSizeImage);
hdr.bfReserved1 = 0;
hdr.bfReserved2 = 0;
// Compute the offset to the array of color indices.
hdr.bfOffBits = (DWORD)sizeof(BITMAPFILEHEADER) + pbih->biSize + pbih->biClrUsed * sizeof (RGBQUAD);
if (WriteFile(hFile, (LPVOID)&hdr, sizeof(BITMAPFILEHEADER),(LPDWORD)&dwTmp, NULL) == FALSE)
return FALSE;
if (WriteFile(hFile, (LPVOID)pbih, sizeof(BITMAPINFOHEADER) + pbih->biClrUsed * sizeof (RGBQUAD), (LPDWORD)&dwTmp, NULL) == FALSE)
return FALSE;
dwTotal = cb = pbih->biSizeImage;
hp = lpBits;
if (WriteFile(hFile, (LPSTR)hp, (int)cb, (LPDWORD)&dwTmp, NULL) == FALSE)
return FALSE;
CloseHandle(hFile);
GlobalFree((HGLOBAL)lpBits);
}
ByteBooster