Precise pixel size for Component

How can I find the exact pixel size for a component?

I’m trying to speed up an operation in paintOverChildren by using an image containing the overlay.

The Component in question is scaled using affineTransformation so getLocalBounds isn’t accurate (also, there’s desktop scaling to consider)

The following doesn’t inspire confidence due to words like ‘aproximate’ :slight_smile:
getApproximateScaleFactorForComponent(this)

Thanks in advance

I know that in the paint call for a component you can get the JUCE: LowLevelGraphicsContext Class Reference from the passed in juce::Graphics and from there you can getPhysicalPixelScaleFactor, would that solve your use case?

1 Like

Brilliant, the following works.
Unfortunately, g.drawImageAt is notably slower than the original code. I’m assuming this is down to scaling the image that this method normally needs to perform.

I’ll keep looking to see if there’s a way of blitting this image more directly.

 const auto scale = g.getInternalContext().getPhysicalPixelScaleFactor();
    if (scale != lastScaleFactor)
    {
        lastScaleFactor = scale;
        const auto cacheRect = (getLocalBounds().toFloat() * scale).getSmallestIntegerContainer();
        overlayCache = Image (Image::ARGB, cacheRect.getWidth(), cacheRect.getHeight(), true);

        Graphics cg{ overlayCache };

        /*
         * OVERLAY DRAW CODE HERE
         e.g. cg.fillRect(...)
         */
    }

    g.drawImageAt (overlayCache, 0, 0);