# Why does drawing an svg path to an Image result in pixelation?

**URL:** <https://forum.juce.com/t/why-does-drawing-an-svg-path-to-an-image-result-in-pixelation/61907>\
**Category:** General JUCE discussion\
**Created:** [June 19, 2024, 2:43am UTC](https://forum.juce.com/t/why-does-drawing-an-svg-path-to-an-image-result-in-pixelation/61907 "2024-06-19T02:43:26Z")\
**Posts on this page:** 5\
**Page:** 1

<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:43am UTC](https://forum.juce.com/t/why-does-drawing-an-svg-path-to-an-image-result-in-pixelation/61907/1 "2024-06-19T02:43:26Z")

</div>

If I draw the path directly to the screen, it’s smooth and perfect.

If I draw the path into an Image, and then copy the Image to the screen, it’s pixelated.

I’m using a Mac Retina. My understanding is that the pixels are twice as many as regular.

But even if I make the image twice the size, and apply an AffineTransform to the Graphics of scale(2.0f) so everything draws twice the size into the image, it’s still pixelated.

---

<div class="post-metadata">

**Author:** ![matt](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/matt/32/18340_2.png) [@matt](https://forum.juce.com/u/matt)\
**Post date:** [June 19, 2024, 3:27am UTC](https://forum.juce.com/t/why-does-drawing-an-svg-path-to-an-image-result-in-pixelation/61907/2 "2024-06-19T03:27:38Z")

</div>

Try applying the transform to the Path first then draw the path on your Image.

Matt

---

<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, 5:43am UTC](https://forum.juce.com/t/why-does-drawing-an-svg-path-to-an-image-result-in-pixelation/61907/3 "2024-06-19T05:43:27Z")

</div>

Thank you. Tried that, didn’t work.

---

<div class="post-metadata">

**Author:** ![QuilioLtd](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/quilioltd/32/15459_2.png) [@QuilioLtd](https://forum.juce.com/u/QuilioLtd)\
**Post date:** [June 24, 2024, 9:18am UTC](https://forum.juce.com/t/why-does-drawing-an-svg-path-to-an-image-result-in-pixelation/61907/4 "2024-06-24T09:18:39Z")

</div>

```auto
void drawHighResolutionPath(Graphics& g, const Path& path)
{
    const float scaleFactor = 2.0f;
    int imageWidth = originalWidth * scaleFactor;
    int imageHeight = originalHeight * scaleFactor;
    Image highResImage = Image (Image::ARGB, imageWidth, imageHeight, true);
    Graphics imageGraphics (highResImage);
    imageGraphics.addTransform (AffineTransform::scale (scaleFactor));
    imageGraphics.setColour (Colours::black);
    imageGraphics.fillPath (path);
    g.drawImageTransformed (highResImage, AffineTransform::scale (1.0f / scaleFactor));
}

```

You’re doing exactly this, right?  
How exactly are you copying the image to the screen?

---

<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 24, 2024, 1:42pm UTC](https://forum.juce.com/t/why-does-drawing-an-svg-path-to-an-image-result-in-pixelation/61907/5 "2024-06-24T13:42:24Z")

</div>

> [@QuilioLtd](#):
>
> You’re doing exactly this, right?

The only difference was in my last step, instead of using

```auto
g.drawImageTransformed (highResImage, AffineTransform::scale (1.0f / scaleFactor));

```

I was using:

```auto
g.drawImage(myImage, getLocalBounds().toFloat());

```

…which should essentially do the same thing, as my Image size was 2x the localBounds.

However, the solution seems to be related to the fact that my component doing the drawing is part of my mainComponent, and the main component can be scaled, so I was using it at 150%, which increases the actual pixel resolution of my Mac from 2.0 scaleFactor to 3.0 scaleFactor. And if I had my GUI at 200%, then the scaleFactor needs to be 4.0. The answer was located in this thread below, but the basic idea is this:

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

```

> [@Quality difference when drawing to image vs drawing to screen](https://forum.juce.com/t/quality-difference-when-drawing-to-image-vs-drawing-to-screen/60913):
>
> 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!
