Pages

Men

rh

12/18/2012

SAVING WINDOWS AS BITMAP in VC++


BOOL SaveBitmap(HDC hDC,HBITMAP hBitmap,char *czFile)
{
FILE * fp= NULL;
fp = fopen(czFile,"wb");
if(fp == NULL)
return false;

BITMAP Bm;
BITMAPINFO BitInfo;
ZeroMemory(&BitInfo, sizeof(BITMAPINFO));
BitInfo.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
BitInfo.bmiHeader.biBitCount = 0;

if(!::GetDIBits(hDC, hBitmap, 0, 0, NULL, &BitInfo, DIB_RGB_COLORS))
return (false);

Bm.bmHeight = BitInfo.bmiHeader.biHeight;
Bm.bmWidth = BitInfo.bmiHeader.biWidth;

BITMAPFILEHEADER BmHdr;

BmHdr.bfType = 0x4d42; // 'BM' WINDOWS_BITMAP_SIGNATURE
BmHdr.bfSize = (((3 * Bm.bmWidth + 3) & ~3) * Bm.bmHeight)
+ sizeof(BITMAPFILEHEADER)
+ sizeof(BITMAPINFOHEADER);
BmHdr.bfReserved1 = BmHdr.bfReserved2 = 0;
BmHdr.bfOffBits = (DWORD) sizeof(BITMAPFILEHEADER)
+ sizeof(BITMAPINFOHEADER);

BitInfo.bmiHeader.biCompression = 0;
// Writing Bitmap File Header ////
fwrite(&BmHdr,sizeof(BITMAPFILEHEADER),1,fp);

fwrite(&BitInfo.bmiHeader,sizeof(BITMAPINFOHEADER),1,fp);

BYTE *pData = new BYTE[BitInfo.bmiHeader.biSizeImage + 5];
if(!::GetDIBits(hDC, hBitmap, 0, Bm.bmHeight,
pData, &BitInfo, DIB_RGB_COLORS))
return (false);
if(pData != NULL)
fwrite(pData,1,BitInfo.bmiHeader.biSizeImage,fp);

fclose(fp);
delete (pData);
return (true);
}
int SaveWindowImage(HWND hWnd, char *czFile)
{
HDC hDC;
RECT r;
SIZE size;

hDC = GetWindowDC(hWnd);
GetWindowRect(hWnd,&r);
size.cx = r.right-r.left;
size.cy = r.bottom-r.top;
HBITMAP hBitmap = CreateCompatibleBitmap(hDC, size.cx, size.cy);
HDC hMemDC = CreateCompatibleDC(hDC);

if (hBitmap){
HBITMAP hOld = (HBITMAP) SelectObject(hMemDC, hBitmap);
BitBlt(hMemDC, 0, 0, size.cx, size.cy, hDC, 0, 0, SRCCOPY);
// Save the captured Bitmap to a file
SaveBitmap(hMemDC,hBitmap,czFile);
DeleteDC(hMemDC);
DeleteObject(hBitmap);
}
ReleaseDC(NULL, hDC);
return 0;
}

No comments :

Post a Comment