"drawImageAt" isn't working - what's wrong?

Sometimes I struggle with what seems like it should be SIMPLE!

I am trying to create a background image for my plugin using the following code and it’s not working - I get a BLACK background and the rest of the graphics (text, etc.) are scrambled all over the window.

What’s wrong here?

void MyPluginAudioProcessorEditor::paint (Graphics& g)
{
Image GUIbackground = ImageFileFormat::loadFrom(juce::File(“C:\MyPlugin\Source\GUI.jpg”));

g.drawImageAt(GUIbackground, 0, 0);

…other lines for text,etc.

Your image might not even get loaded because you have the single \ characters for the file name in the source code. On Windows you should use \\ or / instead.

Also it’s not a great idea to load your image over and over again at each paint call. It would be recommended to have the Image instance as a class member variable and load the image from disk for example in your GUI class constructor.

2 Likes

Understand, was just trying to get it to work first.

Just following examples I found elsewhere! Will change and try it out.

Thank you.

There is a helper for the issue, Xenakios pointed out, not loading the image over and over:
Simply use the ImageCache::getFromFile()

The Image is actually a wrapper, and the image data is shared, so you can create an Image object on the stack at little cost. Use this code:

File backgroundFile {“C:\\MyPlugin\\Source\\GUI.jpg”};
// for debugging:
if (backgroundFile.existsAsFile()) 
{
    DBG ("File exists: " << backgroundFile.getFullPath());
}
auto GUIbackground = ImageCache::getFromFile (backgroundFile);
g.drawImageAt (GUIbackground, 0, 0);

I am actually a fan of using File::getSpecialLocation(), that is much more foolproof:

auto backgroundFile = File::getSpecialLocation (File::userDesktopDirectory).getChildFile ("GUI.jpg");

Hope that helps

1 Like

It works. Thank you!