# Quality difference when drawing to image vs drawing to screen

**URL:** <https://forum.juce.com/t/quality-difference-when-drawing-to-image-vs-drawing-to-screen/60913>\
**Category:** Development\
**Created:** [April 16, 2024, 3:39pm UTC](https://forum.juce.com/t/quality-difference-when-drawing-to-image-vs-drawing-to-screen/60913 "2024-04-16T15:39:45Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![bsumma](https://avatars.discourse-cdn.com/v4/letter/b/df788c/32.png) [@bsumma](https://forum.juce.com/u/bsumma)\
**Post date:** [April 16, 2024, 3:39pm UTC](https://forum.juce.com/t/quality-difference-when-drawing-to-image-vs-drawing-to-screen/60913/1 "2024-04-16T15:39:45Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![reuk](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/reuk/32/21494_2.png) [@reuk](https://forum.juce.com/u/reuk)\
**Post date:** [April 16, 2024, 4:58pm UTC](https://forum.juce.com/t/quality-difference-when-drawing-to-image-vs-drawing-to-screen/60913/2 "2024-04-16T16:58:47Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![stephenk](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/stephenk/32/15198_2.png) [@stephenk](https://forum.juce.com/u/stephenk)\
**Post date:** [June 19, 2024, 2:39am UTC](https://forum.juce.com/t/quality-difference-when-drawing-to-image-vs-drawing-to-screen/60913/3 "2024-06-19T02:39:31Z")

</div>

Is there a way to find out the physical pixel size of a component, based on the current high-resolution screen?

---

<div class="post-metadata">

**Author:** ![fbecir](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/fbecir/32/12998_2.png) [@fbecir](https://forum.juce.com/u/fbecir)\
**Post date:** [June 19, 2024, 5:52am UTC](https://forum.juce.com/t/quality-difference-when-drawing-to-image-vs-drawing-to-screen/60913/4 "2024-06-19T05:52:35Z")

</div>

Hello

Perhaps you can use the Display struct ([JUCE: Displays::Display Struct Reference](https://docs.juce.com/master/structDisplays_1_1Display.html)). You can find the DPI of the display and the scale factor.

---

<div class="post-metadata">

**Author:** ![reuk](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/reuk/32/21494_2.png) [@reuk](https://forum.juce.com/u/reuk)\
**Post date:** [June 19, 2024, 10:54am UTC](https://forum.juce.com/t/quality-difference-when-drawing-to-image-vs-drawing-to-screen/60913/5 "2024-06-19T10:54:56Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![adamwilson](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/adamwilson/32/1387_2.png) [@adamwilson](https://forum.juce.com/u/adamwilson)\
**Post date:** [June 20, 2024, 1:39pm UTC](https://forum.juce.com/t/quality-difference-when-drawing-to-image-vs-drawing-to-screen/60913/6 "2024-06-20T13:39:32Z")

</div>

Is there a way to reliably get this on Windows when running as a plugin?

It always seems to come back with 1.0 in that scenario.

---

<div class="post-metadata">

**Author:** ![stephenk](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/stephenk/32/15198_2.png) [@stephenk](https://forum.juce.com/u/stephenk)\
**Post date:** [June 20, 2024, 4:04pm UTC](https://forum.juce.com/t/quality-difference-when-drawing-to-image-vs-drawing-to-screen/60913/7 "2024-06-20T16:04:27Z")

</div>

> [@adamwilson](#):
>
> It always seems to come back with 1.0 in that scenario.

Are you testing on JUCE 8? I just tested this on JUCE 7.0.12, using my plugin in the AudioPluginHost. It _is_ being accurately reported.

---

<div class="post-metadata">

**Author:** ![stephenk](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/stephenk/32/15198_2.png) [@stephenk](https://forum.juce.com/u/stephenk)\
**Post date:** [June 20, 2024, 4:06pm UTC](https://forum.juce.com/t/quality-difference-when-drawing-to-image-vs-drawing-to-screen/60913/8 "2024-06-20T16:06:46Z")

</div>

@reuk - thanks, so would an approach like this be the best? (it seems to work)

```auto

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());
}

```

---

<div class="post-metadata">

**Author:** ![adamwilson](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/adamwilson/32/1387_2.png) [@adamwilson](https://forum.juce.com/u/adamwilson)\
**Post date:** [June 21, 2024, 7:47am UTC](https://forum.juce.com/t/quality-difference-when-drawing-to-image-vs-drawing-to-screen/60913/9 "2024-06-21T07:47:45Z")

</div>

Testing in Studio One on Windows with JUCE 7.0.10, I always get 1.0 returned. I bodged it by always setting it to 2.0 if 1.0 is returned on Windows.
