Image is not draw in the "paint" method. (Noob)

Hi, i’m very new on JUCE (in fact, this is my first time), and not an c++ expert, i just clicked the “GUI” template on JUCE and began testing some options.
I saw people create a ImageComponent to add image, but i’m not sure that i want to create components for all images i want to add, so i tried to draw the image directly by the “paint” function, the code is like this:

void MainComponent::paint (juce::Graphics& g)
{
g.setColour(Colours::white);
g.fillAll(Colours::white);
File Flower(“C:/TEST2.bmp”);
Image FlowerIMG = ImageCache::getFromFile(Flower);
//Image FlowerIMG = ImageFileFormat::loadFrom(Flower);
g.setFont(Font(16.0f));
Colour Pink(255, 0, 127);
g.setColour(Pink);
g.drawText(“Hello World!”, getLocalBounds(), Justification::centred, true);
g.setOpacity(1.0f);
g.drawImageAt(FlowerIMG, 10, 10, 0);
}


As you can see, i’m just figuring things out in the “hello world” template.
The code compiles successfully, but the screen don’t draws my beautiful flower.
Should i use ImageComponent?
Maybe this code makes no sense, as i said, i’m kinda new in c++…

It would appear to be that Juce doesn’t support .bmp images. Try a .png or .jpg file instead.

1 Like

Thank you!
It was exactly that! Damn, i was struggling with such a small issue! :frowning: I don’t know why i have not tested other formats!
helloflower

1 Like

Actually, this was kind of news to me too. BMP is such a simple format that I would have expected Juce to support it. (I did some test code that tried loading a .bmp image and it failed…)

1 Like

Yeah, perhaps the Juce team thought it smelled too much of Microsoft! :smiley:
PNG is great though, and it’s not a lossy compression, so it’ll not make any difference to quality.

1 Like

Hahaha!
:grin:
Indeed, i’ll be saving the images in PNG for now on. I’m using CImg togheter with JUCE. :stuck_out_tongue:

Your art program should be able to save the PNGs and Projucer can load them in as a resource.
You just need to access the file as a resource in code:

Image i_Background = ImageCache::getFromMemory(BinaryData::Backround_png, (size_t)BinaryData::Backround_pngSize);
1 Like

Well, the official statement was issued more than 10 years ago! :wink:

3 Likes

Thank you for this line!

Kinda off topic, but, is there a way to create and image from a data vector?
I mean, i’m using this CImg and it creates and image object that i can save as PNG, BMP or JPG, but, if i use the line of code you just sent me i would have to first load into the resources, don’t?
Isn’t there a way to me to extract the data of “CImg” object and transform in “juce::Image” object? I think i can use the “setPixelColour” method of BitmapData… But the reference said i should “really know what i’m doing”. :stuck_out_tongue:

Also, is there no reference for “BinaryData”?

Anyway, again, thanks for the tip!

You probably need to get more familiar with Projucer first.

Bring up the File Explorer in Projucer.
Right-click ‘Source’ and ‘Add new Group’
Name your group and move the folder to above ‘Source’ to make things clearer.
Right-click the new group and ‘Add Existing Files’ then select your PNG file.

This will add the png to the project. This will automatically go in you project as a memory resource as a part of BinaryData.h.

This is where the above code can access the block of data with getFromMemory access.
You can add any file to the Projucer this way, including text, waveform files etc.

I’m sorry but I’m not familiar with CImg and ‘data vector’ objects.

1 Like

Don’t worry, you already helped a lot and thank you very much for that!

But if you are curious:
CImg is a very simple header for image processing.
You open or create an image with an object called “CImg”.
This object have a method to save in bmp, png and etc… So i was thinking about saving the files in the program folder and use JUCE to call it, but if there was a way to get directly from the CImg memory and transfer to the juce::Image it would be way simpler, but nvm about that, i’ll figure it out by time. Probably using pointers and stuff…

Again, thanks for all the help.

The function ImageCache::getFromMemory() can load from any valid memory location. It just takes a location and a size as its parameters. So, if your CImg class has the ability to write the image as a PNG, but in memory instead of to a physical file, then you can use that memory location. But since we have no idea what the interface or abilities of a CImg object are, we can’t say if it provides a way to access its image data in a format that juce::Image can handle. Not sure why you want to use an external library for image handling, exactly, or if you need to do so in the same application, or separate apps, so I can’t say if writing to a file using your CImg class and at some point later in time loading it into a (different?) JUCE app is what you need or not.

1 Like

Juce of course also has support for saving image files from an Image object (except those .bmp files :wink: ), would that work for you instead of having to use the external CImg class?

1 Like

Don’t worry, if i could open directly from CImg memory would just be a bonus, at least i can open the PNG i save from it by JUCE and that is good enough.
I’ll check out this getFromMemory method.

Not sure why you want to use an external library for image handling

Because CImg have some methods alike “photoshop” if you know what i mean, but CImg is more about manipulating image, if i had to make a GUI with it, i would have to make it almost from scratch, so, using JUCE for the GUI to manipulate the methods i use in CImg would be very cool.

But don’t worry about that, what i wanted was to open the file and it was just this bmp issue. :stuck_out_tongue:

Thank you all!

I mean, i’m saving from CImg and opening with JUCE, but i wanted to not need to save the image and display it one JUCE, but don’t worry about that, it is just a small issue that don’t matter much, the main problem i had was opening that image and it was solved, so, thanks everybody!