grabScreen and saveImage, image is distorted

Hello everybody!

I discovered openframeworks last week and thought it would be an excellent option to re-write an application i’ve made in java to capture the screen and calculate an pixel average. The reason I wish to use C++ is because the java application had an average of 20fps, which is too slow for my purposes.

But I’ve encountered a very strange error. When running this code

  
  
		int width, height;  
  
		ofImage imageData;  
  
		width = 600;  
		height = 600;  
  
		imageData.allocate(width,height,OF_IMAGE_COLOR_ALPHA);  
  
		imageData.grabScreen(0,0,width,height);  
		imageData.saveImage("/Users/unknownsoldier/Documents/test.png");  
  

the resulting image looks like the attached image, distorted.

so I was wondering if anybody could help me?

I’m running of 0.07 on a macbook pro with xcode 3.2.6 and my target sdk is set to 10.6 since I am running snow leopard.

Thank you in advance!

where do you have that code, in setup? grabScreen is meant to be used after drawing. that image seems to be garbage in the graphics card memory cause you’ve drawn nothing yet?

Thank you for the reply.

Sorry, I forgot to mention where I tried it. I’ve tried running the code snippet inside setup, update and draw.

When using it in setup I got nothing at all, but in update and draw I get the same result as the attached image.

In my setup I run

  
  
  
	ofSetBackgroundAuto(true);  
	ofBackground(255, 0, 255, 255);  
  

And in draw or update I don’t call anything else than the previously cited code. And i’ve tried both true and false in ofSetBackgroundAuto()

Best Regards
Mogrol

it could also be some problem with RGBA images, can you try setting the image with OF_IMAGE_COLOR instead of OF_IMAGE_COLOR_ALPHA see if that works?

I tried this code in draw()

  
  
  
	int width, height;    
	  
	ofImage imageData;    
	  
	width = ofGetScreenWidth();  
	height = ofGetScreenHeight();  
	  
	imageData.allocate(width,height,OF_IMAGE_COLOR);  
	  
	imageData.grabScreen(0,0, width, height);    
	imageData.saveImage("/Users/unknownsoldier/Documents/test.png");  
  

and this worked better… but it only gave me a screen shot of the windoow of my program, and the rest was black.

in setup I ran

  
  
  
	ofSetBackgroundAuto(true);  
	ofBackground(255, 0, 255, 255);  
  

yes grabScreen only grabs the output of your program not the whole screen

Oh, ok… thanks for clearing that up! Now I understand why it did not work :slight_smile:

Do you possibly know if there is any way to grab the entire screen using OF or any other way? The program I wish to write is for an Ambilight project I am doing so I need to grab the entire screen and then analyze blocks of it to get an average color value.

Best Regards
Mogrol