home/ atoms/ of-screenshot-grabscreen

ofImage.grabScreen() captures the current frame to a PNG in bin/data

openFrameworks provides a one-shot screen capture via ofImage::grabScreen(x, y, width, height), which copies the framebuffer region into an ofImage object. Calling save(filename) on that object writes a PNG to the project’s bin/data directory. The canonical pattern binds this to a keyPressed() handler so capture is on-demand. grabScreen() reads the GPU framebuffer after draw() has finished — placing it inside draw() itself would create an ordering conflict.

Examples

// ofApp.h: ofImage img; void ofApp::keyPressed(int key){ if(key == ‘x’){ img.grabScreen(0, 0, ofGetWidth(), ofGetHeight()); img.save(“screenshot.png”); } }

Assessment

Why does grabScreen() go inside keyPressed() rather than inside draw()? What folder does the file land in, and why?

“After adding this to any of your apps, press 'x' and a screenshot of your work will save to the bin >> data folder within your specific app folder.”