Hello all, I’m making an image viewer where it would be useful for me to have in memory an image buffer of what is being displayed on the screen. So, my pipeline right now is:
Create an image that is the size of the Component
Draw directly into the image
Draw this new image in the Component
This looks lower quality than drawing my image directly to the screen. Perceptually blurry. I’m wondering if that is to be expected, or am I missing something. Thanks!
If you’re using a high resolution screen, then the logical size of the component reported by getLocalBounds() will be different to the size of the component in physical pixels. When saving the component to an image, you would need to draw the component at a larger scale, so that the stored image has the correct size in physical pixels.
If you’re inside a paint call, you can find the current scale by calling g.getInternalContext().getPhysicalPixelScaleFactor(). If you use this value, then you’re guaranteed to draw at the same scale as the rest of the JUCE window. Querying the current Displays is another option, but this can get tricky in some cases. When the window spans two displays, we want the OS to pick the best scale factor to use, rather than trying to work this out manually.
@reuk - thanks, so would an approach like this be the best? (it seems to work)
void paint (Graphics& g)
{
auto scaleFactor = g.getInternalContext().getPhysicalPixelScaleFactor();
auto imageBounds = getLocalBounds().toFloat() * scaleFactor;
g.saveState(); // save the current Graphics state
// create an image to draw into with the specifed dimensions
myImage = Image(Image::ARGB, (int) imageBounds.getWidth(), (int) imageBounds.getHeight(), true);
// create a new Graphics that will draw into myImage
Graphics tmp (myImage);
// add a transform so everything will be drawn at the size corresponding to the scaleFactor
tmp.addTransform(AffineTransform::scale(scaleFactor));
//***** do the drawing here ******//
g.restoreState(); // restore graphics state
// copy the image to the screen at the normal size
g.drawImage(myImage, getLocalBounds().toFloat());
}