Guest User

Untitled

a guest
Jul 20th, 2018
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.75 KB | None | 0 0
  1. Mat hwnd2mat(HWND hwnd) {
  2. HDC hwindowDC, hwindowCompatibleDC;
  3. int height, width, srcheight, srcwidth;
  4. HBITMAP hbwindow;
  5. Mat src;
  6. BITMAPINFOHEADER bi;
  7. hwindowDC = GetDC(hwnd);
  8. hwindowCompatibleDC = CreateCompatibleDC(hwindowDC);
  9. SetStretchBltMode(hwindowCompatibleDC, COLORONCOLOR);
  10. RECT windowsize; // get the height and width of the screen
  11. GetClientRect(hwnd, &windowsize);
  12. srcheight = windowsize.bottom;
  13. srcwidth = windowsize.right;
  14. height = windowsize.bottom / 1; //change this to whatever size you want to resize to
  15. width = windowsize.right / 1;
  16. src.create(height, width, CV_8UC4);
  17. // create a bitmap
  18. hbwindow = CreateCompatibleBitmap(hwindowDC, width, height);
  19. bi.biSize = sizeof(BITMAPINFOHEADER); //http://msdn.microsoft.com/en-us/library/windows/window/dd183402%28v=vs.85%29.aspx
  20. bi.biWidth = width;
  21. bi.biHeight = -height; //this is the line that makes it draw upside down or not
  22. bi.biPlanes = 1;
  23. bi.biBitCount = 32;
  24. bi.biCompression = BI_RGB;
  25. bi.biSizeImage = 0;
  26. bi.biXPelsPerMeter = 0;
  27. bi.biYPelsPerMeter = 0;
  28. bi.biClrUsed = 0;
  29. bi.biClrImportant = 0;
  30. SelectObject(hwindowCompatibleDC, hbwindow);
  31. StretchBlt(hwindowCompatibleDC, 0, 0, width, height, hwindowDC, 0, 0, srcwidth, srcheight, SRCCOPY); //change SRCCOPY to NOTSRCCOPY for wacky colors !
  32. GetDIBits(hwindowCompatibleDC, hbwindow, 0, height, src.data, (BITMAPINFO *)&bi, DIB_RGB_COLORS); //copy from hwindowCompatibleDC to hbwindow
  33. DeleteObject(hbwindow);
  34. DeleteDC(hwindowCompatibleDC);
  35. ReleaseDC(hwnd, hwindowDC);
  36. return src;
  37. }
  38.  
  39. int main() {
  40. HWND hwndDesktop = GetDesktopWindow();
  41. Mat src = hwnd2mat(hwndDesktop);
  42. imwrite("output.bmp", src);
  43. return 0;
  44. }
Add Comment
Please, Sign In to add comment