Loading images


#1

Hi,
I am trying to read and display an image and I can’t understand why this is not working. So this is what I am doing.

  1. I have a png file of a knob saved on my desktop.
  2. I created an assets folder in the introjucer and saved the png file there too.

In my PluginEditor.h

private:
Image knob;
ImageComponent imageComponent;

Then in the PluginEditor.cpp, I have this in the constructor

knob = ImageFileFormat::loadFrom(File::File("…/…/…/…/Desktop/Knob.png"));
if (knob.isValid())
imageComponent.setImage(knob);
addAndMakeVisible(&imageComponent);
}

And in the resized function I have
void ProjectAudioProcessorEditor::resized()
{
imageComponent.setBounds(10, 10, 50, 50);
}

So, I ran it a bunch of times and nothing shows up. And setting breakpoints to check if knob loaded correctly, it is null. Can you guys help me? I am stuck. Why is the knob null?

Thanks in advance!


#2

Have you tried an absolute path to the .png file? (Relative paths are notoriously wonky to work with, they often don’t at runtime point to where you think they are pointing to…)


#3

O.M.G!

I just tried it and it works. THANK YOU SO MUCH!!! :blush:


#4

By the way, if you need to programmatically form an absolute path to some special location, it can be done like this :

File fileondesktop = File::getSpecialLocation(File::SpecialLocationType::userDesktopDirectory).getChildFile(“image.png”);