Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- unsigned int takeScreenShot(int startx, int starty,int endx,int endy, int skipPx, bool showPic){
- unsigned int ret = 0;
- HWND hDesktopWnd = NULL;
- HDC hDesktopDC = NULL;
- HDC hCaptureDC = NULL;
- HBITMAP hCaptureBitmap = NULL;
- hDesktopWnd = GetDesktopWindow();
- hDesktopDC = GetDC(hDesktopWnd);
- if (!hDesktopDC) {
- //MessageBox( NULL, L"no hdc screen!", L"Error", MB_OK|MB_ICONERROR);
- ret |= (unsigned char) 1;
- return ret;
- }
- hCaptureDC = CreateCompatibleDC(hDesktopDC);
- if (!hCaptureDC) {
- //MessageBox( NULL, L"no compatible dc screen!", L"Error", MB_OK|MB_ICONERROR);
- ret |= (unsigned char) 2;
- return ret;
- }
- hCaptureBitmap = CreateCompatibleBitmap(hDesktopDC, (endx - startx), (endy - starty));
- if (!hCaptureBitmap) {
- //MessageBox( NULL, L"no bitmap!", L"Error", MB_OK|MB_ICONERROR);
- ret |= (unsigned char) 3;
- return ret;
- }
- SelectObject(hCaptureDC, hCaptureBitmap);
- if(!BitBlt(hCaptureDC, 0, 0, (endx - startx), (endy - starty), hDesktopDC, startx, starty, SRCCOPY | CAPTUREBLT)){
- //MessageBox( NULL, L"BitBlt() failed", L"Error", MB_OK|MB_ICONERROR);
- ret |= (unsigned char) 4;
- return ret;
- }
- BITMAPFILEHEADER bmfHeader;
- BITMAPINFOHEADER bi;
- DWORD dwBmpSize = (endx - startx) * 3 * (endy - starty);
- bi.biSize = sizeof(BITMAPINFOHEADER);
- bi.biWidth = (endx - startx);
- bi.biHeight = (endy - starty);
- bi.biPlanes = 1;
- bi.biBitCount = 24;
- bi.biCompression = BI_RGB;
- bi.biSizeImage = 0;
- bi.biXPelsPerMeter = 0;
- bi.biYPelsPerMeter = 0;
- bi.biClrUsed = 0;
- bi.biClrImportant = 0;
- char *lpbitmap = new char[dwBmpSize];
- GetDIBits(hCaptureDC, hCaptureBitmap, 0, (UINT)(endy - starty), lpbitmap, (BITMAPINFO *)&bi, DIB_RGB_COLORS);
- unsigned char R = colAvr((unsigned char*)lpbitmap, COLOR_R, dwBmpSize, skipPx);
- unsigned char G = colAvr((unsigned char*)lpbitmap, COLOR_G, dwBmpSize, skipPx);
- unsigned char B = colAvr((unsigned char*)lpbitmap, COLOR_B, dwBmpSize, skipPx);
- ret = (R << 24);
- ret |= (G << 16);
- ret |= (B << 8);
- ret |= (unsigned char) 0;
- if(showPic){
- FILE *fp;
- errno_t err;
- if((err = fopen_s(&fp, "c:\\cica.bmp", "wb")) != NULL){
- ret |= (unsigned char) 5;
- return ret;
- }
- DWORD dwSizeofDIB = dwBmpSize + sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER);
- bmfHeader.bfOffBits = (DWORD)sizeof(BITMAPFILEHEADER) + (DWORD)sizeof(BITMAPINFOHEADER);
- bmfHeader.bfSize = dwSizeofDIB;
- bmfHeader.bfType = 0x4D42; //BM
- fwrite((unsigned char*)&bmfHeader, sizeof(unsigned char), sizeof(BITMAPFILEHEADER), fp);
- fwrite((unsigned char*)&bi, sizeof(unsigned char), sizeof(BITMAPINFOHEADER), fp);
- fwrite((unsigned char*)lpbitmap, sizeof(unsigned char), dwBmpSize, fp);
- fclose(fp);
- }
- delete lpbitmap;
- ReleaseDC(hDesktopWnd,hDesktopDC);
- DeleteDC(hCaptureDC);
- DeleteObject(hCaptureBitmap);
- return ret;
- }
Advertisement
Add Comment
Please, Sign In to add comment