How to take screenshots with ofFbo?

Hi fellow oF users,

I’m trying to draw my screen to an ofFbo and save a picture from it.
This is for generating a screenshot with a doubled resolution of my screen.

Here’s my code in ofApp::setup()

fbo.allocate(ofGetScreenWidth() * 2, ofGetScreenHeight() * 2, GL_RGBA); // fbo is an ofFbo
img.allocate(ofGetScreenWidth() * 2, ofGetScreenHeight() * 2, ofImageType::OF_IMAGE_COLOR_ALPHA); // img is an ofImage

Here’s my code in ofApp::draw():

fbo.begin();
ofScale(2, 2, 2);
easyCam.begin(); // easyCam is an ofEasyCam
// ... all the drawings
easyCam.end();
fbo.end();

ofPixels pixels;
fbo.readToPixels(pixels);
img.setFromPixels(pixels);
img.saveImage("screenshot.png");

But this just saves a pitch black image (black is the background color of the program set with ofBackground()). The size is correct though.

What’s the problem here?

1 Like

See: http://www.openframeworks.cc/documentation/graphics/ofImage.html#show_ofSaveImage

You already have an ofPixels instance, so you could use that in the global ofSaveImage function.

ofSaveImage(pixels,"somePath");

@amnon

Hmm… still getting the same result.

Does fbo.draw(0,0) render what you want to save? It might be that you’re not calling ofClear() to clear the fbo before each render.

Also, what’s the smile variable you’re setting the img with? You should be using pixels. Might be a typo.

@chuckleplant

No, it doesn’t render what I want to save. I get a pitch black screen - same as what I get when I save the image.

Oh, the smile thing is just a typo. I typed pixels in my actual code. Sorry.

Hi there!

Just to make sure of what @chuckleplant wrote, are you calling ofClear() after fbo.begin()?

@chuckleplant @hubris

Added ofClear(). Now perfectly working! Thanks!

1 Like