Trouble getting dynamically created image to an ImageButton

I am drawing on a small image and then trying to transfer that to same size ImageButton. When I load an image from Binary Resource to the button it shows fine, but for some reason I am missing something when trying to draw and then transfer the image to my button.

// From header file
private:
Image loadImage, plot;
ImageButton loadButton, myImageButton;

DetailLFO::DetailLFO()
       {        
          loadImage = ImageCache::getFromMemory (
            BinaryData::buttonload_png, BinaryData::buttonload_pngSize);

      loadButton.setImages (false, true, true, loadImage, 0.75f, 
        Colours::transparentBlack, loadImage, 1.0f, Colours::transparentWhite, 
        loadImage, 1.0f, Colours::transparentBlack);

     int buttonSize = 64;
     plot = Image (Image::PixelFormat::RGB, buttonSize, buttonSize / 2, false);
                  
     // Clear image
     plot.clear (Rectangle<int> (0, 0, buttonSize, buttonSize / 2), Colour (0x000000));
                 
     // Draw image
     for (int i = 0; i < buttonSize; i++)
      	plot.setPixelAt (i, i / 2, Colours::green);
     
     myImageButton.setComponentID ("Saw waveform");
      
     myImageButton.setImages (false, true, true, plot, 0.75f, 
            Colours::transparentBlack, plot, 1.0f, Colours::transparentWhite, 
            plot, 1.0f, Colours::transparentBlack);
                  		
     addAndMakeVisible (myImageButton);
}

I position the button in “resized()” and as I described if I instead replace the “plot” image with a binary resource image, it shows fine.

I don’t know where this code is, but is plot going out of scope? You’ve declared it locally. Maybe declaring it as a member instead will fix this?

It’s in the Constructor. Besides I only use plot temporarily, it is not needed in the global scope.

You call myImageButton.setImages, passing it the local “plot” variable. If that variable goes out of scope, are you sure that the image is still part of the button? I haven’t code-dived to see if it is copied or referred to, but if it’s not physically copied, then when it goes out of scope, the button will no longer have an image to refer to.

Ok I can see where you are going, let me try to change a few things, thanks.

Well there is no change if I declare plot in my header.

private:
    Image loadImage, plot;
    ImageButton loadButton;

Also in my header I declare some other image buttons, which are also loaded in my “Constructor” and as explained they work fine.

DetailLFO::DetailLFO()
 {                
    loadImage = ImageCache::getFromMemory (
      BinaryData::buttonload_png, BinaryData::buttonload_pngSize);

    loadButton.setImages (false, true, true, loadImage, 0.75f, 
      Colours::transparentBlack, loadImage, 1.0f, Colours::transparentWhite, 
      loadImage, 1.0f, Colours::transparentBlack);
}

You are not declaring your loadImage variable locally in that code snippet. So it’s not going out of scope, and is thus not the same thing.

Did you remove the word “Image” from this line?

     Image plot = Image (Image::PixelFormat::RGB, buttonSize, buttonSize / 2, false);

It should just be:

plot = Image (Image::PixelFormat::RGB, buttonSize, buttonSize / 2, false);

The function setImages() assigns the button’s normalImage using the = operator, and the docs say this about that function:

/** Makes this image refer to the same underlying image as another object.

    This won't create a duplicate of the image - when Image objects are copied, they simply
    point to the same shared image data. To make sure that an Image object has its own unique,
    unshared internal data, call duplicateIfShared().
*/
Image& operator= (const Image&);

So, like I suspected, that local variable is going out of scope, and thus is no longer valid when the button goes to draw its image.

Yes I just tried that, and changed my original post to reflect. It makes no change, but thanks.

Anyways I complete forgot which special characters to use in this forum, and more specifically post, to mark it as code.

Well, that’s gonna make my comments look strange to someone reading them for the first time… :smiley:

Sorry but just refer anyone accusing you of being strange to me, and I will defend you :wink:

1 Like

I got it to work just by changing;
plot = Image (Image::PixelFormat::RGB, buttonSize, buttonSize / 2, false);

to;
plot = Image (Image::PixelFormat::RGB, buttonSize, buttonSize / 2, true);

Very strange!