getPixelAt() not reflecting changes from drawing directives?

I don’t understand why what we see on screen is not taken into account by getPixelAt(), in the second case, below:

void Canvas::paint (Graphics& g)
g.fillAll (Colours::yellow);

// case one displays the correct value
//tmpImg.setPixelAt(200, 200, Colours::green);
//DBG("pixel brightness: " << tmpImg.getPixelAt(200, 200).getBrightness() );

// case two displays 0
auto fillType = FillType();
g.fillEllipse(0, 0, 500, 500);
DBG("pixel brightness: " << tmpImg.getPixelAt(200, 200).getBrightness() );


Your question doesn’t make sense with the code you posted. You are requesting the pixel from an image (tmpImg), which is not related to the screen. If you were to somehow capture the contents of the screen into an image, you could use that image to examine the screen pixel. If you just want to get the info of what is in a JUCE Component you can use this API:

Image Component::createComponentSnapshot(Rectangle< int > areaToGrab, bool clipImageToComponentBounds=true, float scaleFactor=1.0f)

If you want to grab things outside of your application, then you will need to do something else, which is indicated at the end of this thread:

Right, it did not make sense indeed. The fact is that I made a shortcut for the code and forgot to mention the following: tmpImg is a global variable and in the original code, outside from the paint override, I was using another Graphics object for allowing me to use fillEllipse(). But while this image was properly rendered, using g.drawImage(tmpImg) in paint(), I could not get any pixel from it. Which probably leads us to some other nonsense issue?

The tmpImg is not going to change based on what you do in the Component’s paint() code. What you see on the screen is a separate pixel buffer, your image drawn before is drawn into that. If you really need to be able to get pixels from what is visible on the screen in the component, you may need to do tricks like capture an area of the component as a new Image like cpr suggested above. (And depending how often you would need to do that, it could get pretty inefficient…)

Thanks a lot. My problem might be different (and I hope it is): to sum up, I want to get pixels from an Image on which has been drawn a path with fillEllipse().
I’m a bit confused with this.
Because I’m using another Graphic context: Graphics lg (tmpImg), then I’m using fillEllipse() there, and call repaint().
Therefore in paint(), if i call g.drawImage(tmpImg, 0, 0, 600, 400, 0, 0, 600, 400);
it is effectively painted. So I suppose the image exists there…
But I can’t get pixel data from it.

I am completely confused about what you are trying to do…

To put it simply, let’s say I want to draw an ellipse, and get the pixel data from the resulting image. How should I proceed?

Draw an ellipse where? In an Image instance or directly on the Component in the paint() method?

I’d rather draw it in an Image instance.

This code works as expected :

        Image tempimg{ Image::ARGB,200,200,true };
		Graphics g{ tempimg };
		g.fillEllipse(0.0, 0.0, 200.0, 200.0f);
		Colour c = tempimg.getPixelAt(100, 100);
		jassert(c == Colours::cadetblue);

Yes it works.
I suppose that DBG printing is not an option ?

Of course it is…? I really don’t understand what you are actually trying to do?

Sorry, I messed around with the variables in my DBG line.
I could finally obtain the getPixelAt() working from my image.
Though it might not been necessary for me to call getPixelAt() in another place, that’s what I did ( and in the paint() method! ) . I am still not sure why I can’t get getPixelAt working outside the function where I drew it, but I’ll investigate on this, Thank you.