Screen grabbing
Sometimes it is desirable to save the picture drawn by your project in the file. You can do it using tools of your operating system, but it's more comfortable to do it right in your project. So let's see how to save the contents of your project screen to an image file.
For such purposes, we need to use the ofImage
class for working with images. Though the class is considered in Chapter 4, Images and Textures, for screen grabbing, it is just enough to understand that the ofImage
object holds an image.
The following code saves the current screen to file on the pressing of the Space bar. It should be added to the testApp::keyPressed()
function as follows:
//Grab the screen image to file if ( key == ' ' ) { ofImage image; //Declare image object //Grab contents of the screen image.grabScreen( 0, 0, ofGetWidth(), ofGetHeight() ); image.saveImage( "screen.png" ); //Save image to file }
The parameters of the image.grabScreen()
function specify the rectangle of the grabbing...