1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
|
BOOL SaveBitmap(HDC hDC,HBITMAP hBitmap,char* szPath);
void CaptureActiveWindow(void)
{
RECT ActWndRect;
WCHAR buf [100],buf1[20];
int xSrc=0,ySrc=-19;
int DepcWidth=10, DepcHeight=5;
//OutputDebugString(L"Start capture act window ");
HWND s = GetDesktopWindow();
HDC ActWndDC = GetDC(s); //DC for the window you have clicked on
HDC MemDC = CreateCompatibleDC(ActWndDC); //Memory DC Compatible with Above DC
GetWindowRect(s,&ActWndRect); //Will Store the Windows Are in Rectangle
//wsprintf(buf,L"x1 = %d , y1 = %d, x2 = %d y2 =%d",ActWndRect.left,ActWndRect.top,ActWndRect.right,ActWndRect.bottom);
//OutputDebugString(buf);
int Width = ActWndRect.right-ActWndRect.left + 50; //Width of the Window
int Height =ActWndRect.bottom-ActWndRect.top + 50; //Hight of the Window
//if(GetWindowText(s,buf1,20) >0)
//{
// OutputDebugString(buf1);
//}
HDC DlgDC;
HBITMAP hBitmap = CreateCompatibleBitmap(DlgDC,Width-DepcWidth,Height-DepcHeight);//Will Create Bitmap Comatible With Our Window
SelectObject(MemDC,hBitmap);
BitBlt(MemDC,0,0,Width,Height,ActWndDC,xSrc,ySrc,SRCCOPY);//Will Copy the Window into MemDC
//BitBlt(DeskDC,110,110,Width,Height,MemDC,Begpt.x,Begpt.y,SRCCOPY);
SaveBitmap(MemDC, hBitmap,"Sample.bmp"); // will Save DC into .bmp File
//Will Show u the .bmp File in MSPAINT.
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//DrawBox Will draw the Rectangle when you will Leave the mouse left Button in Selected Area Through Mouse.
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//SavBitmap will Save our DC into .Bmp(sample.bmp) File.
BOOL SaveBitmap(HDC hDC,HBITMAP hBitmap,char* szPath)
{
//OutputDebugString(L"Start SaveBitmap() ");
DWORD error;
FILE * fp= NULL;
fp = fopen(szPath,"wb");
if(fp == NULL)
{
//OutputDebugString(L"Error Unable to Create File ");
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))
{
// OutputDebugString(L"Error GetDIBits Fail");
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 ////
size_t size = fwrite(&BmHdr,sizeof(BITMAPFILEHEADER),1,fp);
if(size < 1)
{
//OutputDebugString(L"Error Header Write");
error = GetLastError();
}
size = fwrite(&BitInfo.bmiHeader,sizeof(BITMAPINFOHEADER),1,fp);
if(size < 1)
{
//OutputDebugString(L"Error Write");
error = GetLastError();
}
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);
}
|