Saving the static control's HDC into bitmap
I'm trying to save into bitmap the contents of a static control:
Code:
int SaveDCToBitmap(HDC hDc, const char *lpszFile, int w, int h) {
HBITMAP hBitmap = CreateCompatibleBitmap(hDc, w, h);
if (hBitmap) {
HANDLE hFile = CreateFile(lpszFile, GENERIC_WRITE, NULL, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
if (hFile != INVALID_HANDLE_VALUE) {
BITMAPFILEHEADER bfh = {0};
BITMAPINFOHEADER bih = {0};
DIBSECTION ds = {0};
DWORD dwResult = 0;
int nImageSize = w * h * 3;
bfh.bfType = MAKEWORD('B','M');
bfh.bfSize = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER) + nImageSize;
bfh.bfOffBits = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER);
WriteFile(hFile, &bfh, sizeof(BITMAPFILEHEADER), &dwResult, NULL);
bih.biSize = sizeof(BITMAPINFOHEADER);
bih.biWidth = w;
bih.biPlanes = 1;
bih.biHeight = h;
bih.biBitCount = 24;
bih.biSizeImage = nImageSize;
bih.biCompression = BI_RGB;
WriteFile(hFile, &bih, sizeof(BITMAPINFOHEADER), &dwResult, NULL);
GetObject(hBitmap, sizeof(DIBSECTION), &ds);
WriteFile(hFile, ds.dsBm.bmBits, nImageSize, &dwResult, NULL);
CloseHandle(hFile);
DeleteObject(hBitmap);
}
}
return 0;
}
// later on a command event
case ID_ARCHIVO_GUARDAR:
{
HDC hdc = GetWindowDC(hStatic);
if (hdc) {
RECT rc = {0};
GetWindowRect(hStatic, &rc);
SaveDCToBitmap(hdc, "G:\\files\\teta.bmp", rc.right-rc.left, rc.bottom-rc.top);
}
ReleaseDC(hStatic, hdc);
break;
}
}
Seems that the dimmensions are ok (in explorer status bar), but the image doesn't show...any ideas?