Grab (capture) screen in Windows 7

#1

Hello all,

I am trying to capture the pixels below the oF window, as discussed in this post: http://forum.openframeworks.cc/t/grabbing-from-the-screen-osx/2946/0. However, I am working on Windows 7 and do not have the Cocoa framework available.

Any suggestions as to implementing screen capture in Win7 would be appreciated.

#2

Hi,

If you don’t find, an alternative solution could be: use a external application to do it. For example: http://code.google.com/p/boxcutter/
I don’t know how to launch an external app, but it must be possible.

#3

wow, i think this will be difficult, since i’m not sure that there is a good screen capture example for OF and windows.

that boxcutter app looks like it’s the closest thing. check out the boxcutter.cpp for a place to get started:

  
  
// Captures a screenshot from a region of the screen  
// saves it to a file  
bool capture_screen(const char *filename, int x, int y, int x2, int y2)  
{  
    // normalize coordinates  
    normalize_coords(&x, &y, &x2, &y2);  
    int w = x2 - x;  
    int h = y2 - y;  
  
    // copy screen to bitmap  
    HDC screen_dc = GetDC(0);  
    HDC shot_dc = CreateCompatibleDC(screen_dc);  
    HBITMAP shot_bitmap =  CreateCompatibleBitmap(screen_dc, w, h);  
    HGDIOBJ old_obj = SelectObject(shot_dc, shot_bitmap);  
      
    if (!BitBlt(shot_dc, 0, 0, w, h, screen_dc, x, y, SRCCOPY)) {  
	printf("error: BitBlt failed\n");  
        return false;  
    }  
      
    // save bitmap to file  
    bool ret = save_bitmap_file(shot_bitmap, shot_dc, filename);  
      
    DeleteDC(shot_dc);  
    DeleteDC(screen_dc);  
    SelectObject(shot_dc, old_obj);  
      
    return ret;  
}  
  

instead of this line:

  
  
    bool ret = save_bitmap_file(shot_bitmap, shot_dc, filename);  
  

you’re going to want to load the data into an ofImage and return it.

#4

also, this thread might be a good place to look http://stackoverflow.com/questions/5069104/fastest-method-of-screen-capturing

#5

kylemcdonald, your replies enlarge my coding vision :slight_smile:

gavinh, this afternoon I discovered how to launch an external program, so I can tell you more now.
If you put boxcutter.exe into your bin folder, you can use this code to capture the screen:

  
  
string fileName = "screenshot.png";  
int x = 10;  
int y = 30;  
int w = 200;  
int h = 150;  
char cmd[300];  
sprintf(cmd, "boxcutter -c %d,%d,%d,%d data\\%s", x, y, w, h, fileName.c_str());  
system(cmd);  

Probably not the best way to do it, but it work.

P.S: you can either install boxcutter in any folder on your hard drive, and add this folder to your path environment variable.

#6

hey lilive – that’s a really smart technique! :slight_smile:

unfortunately i think gavinh is trying to get capture in realtime, something like the xcode example that zach posted, rather than just grabbing a single screenshot :frowning: so constantly saving+compressing images to pngs might be too slow.

#7

Hi. I implemented screen grabbing for Windows (GDI method)
https://github.com/perevalovds/ofxWindows.