Drawing an image

Hello everyone,

I am drawing an image into the destination rectangle (‘getLocalBounds().toFloat()’ ). I specified destX, destY, destWidth and destHeight parameters but I keep getting an error about the arguments being less or many. This is the code which seems right to me but don’t understand why I get this error.

g.drawImage(image1, getLocalBounds().tofloat(), 0, 0, 50, 270);

I loaded the image correctly using getFromMemory. I even tried add true as the last argument, can anyone tell me what I’m doing wrong here?

Thanks in advance

You supply the first target as rectangle from getLocalBounds(), but the function requires them as scalar values:

g.drawImage (image1, 0, 0, getWidth(), getHeight(), 0, 0, 50, 270);

If 50x270 is the dimension of the image, you can use a different overload too, like:

g.drawImage (image1, getLocalBounds().toFloat());

Oh thank you so much @daniel for the quick response that solved my problem.

One last question, If I need to fit an image into a rectangle, this:

Rectangle Rect(0, 100, 144, 144);

I tried this but it doesn’t work:

g.drawImage(image1, getLocalBounds(), Rect);

How would I go about drawing this image in this rectangle. I’m still having a hard time drawing these images.

anyone who can help with this?

You almost had it, but you provide two rectangles, the getLocalBounds() which is the whole component and the Rect.
Try instead:

g.drawImageWithin (image, 0, 100, 144, 144);

There is an additional parameter you can experiment with: RectanglePlacement, which is used to determine if the aspect ratio is allowed to change or what to do if it doesn’t fit.

That is just one of a few options, an overload taking a juce::Rectangle<float> exists as well, see drawImage().

The reason i am not using the coordinates in the image is because i am trying to display an image strip based on the current frame i dont want to draw the entire image.

int frameIndex = 0; // The index of the frame you want to display

    int frameWidth = 44; 
    int frameHeight = imageStrip.getHeight() / numFrames; // Height of each frame
    int sourceX_ = 0; // X-coordinate of the top-left corner of the frame in the image strip
    int sourceY_ = frameIndex * frameHeight; // Y-coordinate of the top-left corner of the frame in the image strip
    int sourceWidth_ = frameWidth; // Width of the frame
    int sourceHeight_ = frameHeight; // Height of the frame
    
    Rectangle<int> frameBounds(0, currentFrame * frameHeight, imageStrip.getWidth(),  frameHeight);

    
    g.drawImage(imageStrip, sourceX_, sourceY_, getWidth(), getHeight(), 0, 0, sourceWidth_, sourceHeight_);

I see, that wasn’t clear to me from your question.

So does the snippet you posted work for you?

No it doesn’t, I still can’t draw the image I keep getting the ‘No member named getLocalBounds’ in juce::Image

I think you are almost there, you just mixed up the order:
destination comes first, then the source coordinates of your film strip.

int frameIndex = 0; // The index of the frame you want to display

int frameWidth = 44; 
int frameHeight = imageStrip.getHeight() / numFrames; // Height of each frame
int sourceX_ = 0; // X-coordinate of the top-left corner of the frame in the image strip
int sourceY_ = frameIndex * frameHeight; // Y-coordinate of the top-left corner of the frame in the image strip
int sourceWidth_ = frameWidth; // Width of the frame
int sourceHeight_ = frameHeight; // Height of the frame

g.drawImage (imageStrip, 0, 0, getWidth(), getHeight(), sourceX_, sourceY_, sourceWidth_, sourceHeight_);

see drawImage()

Thank you Daniel that solves it, I think I’m tired and end up mixing things up.