# Drawing to image with Graphics class broken on Windows with JUCE 8

**URL:** <https://forum.juce.com/t/drawing-to-image-with-graphics-class-broken-on-windows-with-juce-8/63689>\
**Category:** General JUCE discussion\
**Created:** [October 2, 2024, 9:03am UTC](https://forum.juce.com/t/drawing-to-image-with-graphics-class-broken-on-windows-with-juce-8/63689 "2024-10-02T09:03:08Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![nonlethalapplications](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/nonlethalapplications/32/10298_2.png) [@nonlethalapplications](https://forum.juce.com/u/nonlethalapplications)\
**Post date:** [October 2, 2024, 9:03am UTC](https://forum.juce.com/t/drawing-to-image-with-graphics-class-broken-on-windows-with-juce-8/63689/1 "2024-10-02T09:03:08Z")

</div>

This simple code to produce a red output image will not work with JUCE 8 on Windows. It will always result in an “empty” image (black image with JPEG, transparent image with PNG).

It worked well with JUCE 7 on both Windows and Mac and also works with JUCE 8 on macOS.

```auto
Image image(Image::PixelFormat::RGB, 200, 200, true);
Graphics graphicsContext(image);
graphicsContext.setColour(Colours::red);
graphicsContext.fillRect(0, 0, image.getWidth(), image.getHeight());

File outputFile("<some path>/test.jpg");
FileOutputStream stream(outputFile);
JPEGImageFormat jpegFormat;
jpegFormat.writeImageToStream(image, stream);

```

---

<div class="post-metadata">

**Author:** ![thomash](https://avatars.discourse-cdn.com/v4/letter/t/ad7895/32.png) [@thomash](https://forum.juce.com/u/thomash)\
**Post date:** [October 2, 2024, 9:25am UTC](https://forum.juce.com/t/drawing-to-image-with-graphics-class-broken-on-windows-with-juce-8/63689/2 "2024-10-02T09:25:16Z")

</div>

Have a read of the feature overview [JUCE 8 Feature Overview: Direct 2D - JUCE](https://juce.com/blog/juce-8-feature-overview-direct-2d/).

With Direct2D in JUCE 8, graphics contexts do not draw to their images until they go out of scope. According to the overview, wrapping lines 2 to 4 in curly brackets should fix your example.

---

<div class="post-metadata">

**Author:** ![nonlethalapplications](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/nonlethalapplications/32/10298_2.png) [@nonlethalapplications](https://forum.juce.com/u/nonlethalapplications)\
**Post date:** [October 2, 2024, 9:42am UTC](https://forum.juce.com/t/drawing-to-image-with-graphics-class-broken-on-windows-with-juce-8/63689/3 "2024-10-02T09:42:55Z")

</div>

Ah that makes sense!  
Thanks for your help!

I extracted it into a small function and now it works.
