Why Doesn't grabScreen+saveImage Work?

I have a working program drawing to the screen. I decided to add the ability to save the screen as a PNG after a set number of iterations.

Following is the relevant code from my program:

void testApp::setup(){  
    picout.allocate(ofGetScreenWidth(), ofGetScreenHeight(), OF_IMAGE_COLOR);  
void testApp::update(){  
    if(counter = 500) {  
        picout.grabScreen(0,0,picout.width, picout.height);  

To all appearances the program works fine and it creates the correctly sized PNG output file. The problem is the output file is nothing but a black picture (note the screen background color is not black). So why does everything appear to work but I wind up with an empty PNG file?

Note that I have tried moving the grabScreen/saveImage combo around - placing it in different methods. All produce the same correctly sized but empty png.

Please tell me what is wrong with the above code. Note that I am running XP using code::blocks and mingw.

Thanks. Jim

I think it needs to happen inside the draw function?

Hi memphistechno,

I tried saving the file from inside the draw function - same result. I also tried it inside the exit function - same result. I also tried generating the image file in the mouseclicked function with an incremental filename (very handy) but got the same result. The graphics file is written, it is set to the correct pixel dimensions, but is just a solid black picture.

Hasn’t anybody ever tried to save their screen from within their program?

Unfortunately I have not been able to find a single example of grabScreen and saveImage being used to actually save a picture.

Hi all,

FYI - I’ve figured out the source of the problem and the solution BITES.

If you want to save a picture then you have to make sure of the following:


If you set ofSetBackgroundAuto() to false then grabScreen and saveImage output nothing. (no error message either)

In order to mimic the effects of ofSetBackgroundAuto(true) you will need to set up an infinite loop inside of the draw function and then move all your code into there.

Of course the downside of this is that you will never see what you’re drawing since the screen is updated only when you exit the draw function - thus killing your ability to see what is going on and interacting with the screen.

There is one possibility that I haven’t tried yet as a work around. But now it’s off to make dinner.