hello all
I want to save the printer hdc to a image file,and the hdc I get from hook the api EndPage().
the code as follow:
bool SaveToBmp(HDC hdc, RECT rc, SIZE ImgDstSize, LPCWSTR lpFilePath)
{
bool bResult = false;
BITMAPINFO bmpInfo = { 0 };
BYTE *pData = NULL;
SIZE ImgSrcSize = { rc.right - rc.left, rc.bottom - rc.top };
HBITMAP hBmp = NULL;
HGDIOBJ hOldObj = NULL;
HDC hdcMem = NULL;
bmpInfo.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
bmpInfo.bmiHeader.biWidth = ImgDstSize.cx;
bmpInfo.bmiHeader.biHeight = ImgDstSize.cy;
bmpInfo.bmiHeader.biPlanes = 1;
bmpInfo.bmiHeader.biBitCount = 24;
HANDLE hFile = INVALID_HANDLE_VALUE;
BITMAPFILEHEADER bmFileHeader = { 0 };
BITMAPINFOHEADER bmInfoHeader = { 0 };
hdcMem = ::CreateCompatibleDC(NULL);
if (hdcMem == NULL) {
goto _EXIT_FUNC;
}
hBmp = CreateDIBSection(hdcMem, &bmpInfo, DIB_RGB_COLORS, reinterpret_cast<VOID **>(&pData), NULL, 0);
if (hBmp == NULL) {
goto _EXIT_FUNC;
}
hOldObj = SelectObject(hdcMem, hBmp);
::StretchBlt(hdcMem, 0, 0, ImgDstSize.cx, ImgDstSize.cx, hdc, rc.left, rc.top, ImgSrcSize.cx, ImgSrcSize.cy, SRCCOPY);
DWORD dwbmpSize = ImgDstSize.cx * ImgDstSize.cy * 3;
bmInfoHeader.biSize = sizeof(BITMAPINFOHEADER);
bmInfoHeader.biWidth = ImgDstSize.cx;
bmInfoHeader.biHeight = ImgDstSize.cy;
bmInfoHeader.biPlanes = 1;
bmInfoHeader.biBitCount = 24;
bmFileHeader.bfType = 0x4d42;
bmFileHeader.bfOffBits = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER);
bmFileHeader.bfSize = bmFileHeader.bfOffBits + ((bmInfoHeader.biWidth * bmInfoHeader.biHeight) * 3); hFile = CreateFile(lpFilePath,GENERIC_WRITE,0,NULL,CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL,NULL);
if(hFile == INVALID_HANDLE_VALUE) {
goto _EXIT_FUNC;
}
DWORD dwWrite = 0;
WriteFile(hFile,&bmFileHeader,sizeof(BITMAPFILEHEADER),&dwWrite,NULL);
WriteFile(hFile,&bmInfoHeader, sizeof(BITMAPINFOHEADER),&dwWrite,NULL);
WriteFile(hFile,pData, dwbmpSize,&dwWrite,NULL);
SelectObject(hdcMem, hOldObj);
bResult = true;
_EXIT_FUNC:
if (hBmp != NULL) {
::DeleteObject(hBmp);
}
if (hdcMem != NULL) {
::DeleteObject(hdcMem);
}
if (hFile != INVALID_HANDLE_VALUE) {
CloseHandle(hFile);
}
return bResult;
}
If I put in the hdc From the screen hdc ,the program can Get the Image,but if I put in the hdc from hook the EndPage(); the program save a error image.why
Please give me the best solution for this.
thank you!