# Drawing an image

**URL:** <https://forum.juce.com/t/drawing-an-image/58668>\
**Category:** General JUCE discussion\
**Created:** [November 4, 2023, 5:30pm UTC](https://forum.juce.com/t/drawing-an-image/58668 "2023-11-04T17:30:10Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![EmmanWorld](https://avatars.discourse-cdn.com/v4/letter/e/d07c76/32.png) [@EmmanWorld](https://forum.juce.com/u/EmmanWorld)\
**Post date:** [November 4, 2023, 5:30pm UTC](https://forum.juce.com/t/drawing-an-image/58668/1 "2023-11-04T17:30:10Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![daniel](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/daniel/32/790_2.png) [@daniel](https://forum.juce.com/u/daniel)\
**Post date:** [November 4, 2023, 5:38pm UTC](https://forum.juce.com/t/drawing-an-image/58668/2 "2023-11-04T17:38:53Z")

</div>

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

```auto
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:

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

```

---

<div class="post-metadata">

**Author:** ![EmmanWorld](https://avatars.discourse-cdn.com/v4/letter/e/d07c76/32.png) [@EmmanWorld](https://forum.juce.com/u/EmmanWorld)\
**Post date:** [November 4, 2023, 6:02pm UTC](https://forum.juce.com/t/drawing-an-image/58668/3 "2023-11-04T18:02:35Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![EmmanWorld](https://avatars.discourse-cdn.com/v4/letter/e/d07c76/32.png) [@EmmanWorld](https://forum.juce.com/u/EmmanWorld)\
**Post date:** [November 5, 2023, 3:36pm UTC](https://forum.juce.com/t/drawing-an-image/58668/4 "2023-11-05T15:36:30Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![EmmanWorld](https://avatars.discourse-cdn.com/v4/letter/e/d07c76/32.png) [@EmmanWorld](https://forum.juce.com/u/EmmanWorld)\
**Post date:** [November 5, 2023, 7:04pm UTC](https://forum.juce.com/t/drawing-an-image/58668/5 "2023-11-05T19:04:20Z")

</div>

anyone who can help with this?

---

<div class="post-metadata">

**Author:** ![daniel](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/daniel/32/790_2.png) [@daniel](https://forum.juce.com/u/daniel)\
**Post date:** [November 5, 2023, 8:39pm UTC](https://forum.juce.com/t/drawing-an-image/58668/6 "2023-11-05T20:39:26Z")

</div>

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

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

```

There is an additional parameter you can experiment with: [RectanglePlacement](https://docs.juce.com/master/classRectanglePlacement.html), 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()](https://docs.juce.com/master/classGraphics.html#aacaf3a0b803211e9332de2070ead1fda).

---

<div class="post-metadata">

**Author:** ![EmmanWorld](https://avatars.discourse-cdn.com/v4/letter/e/d07c76/32.png) [@EmmanWorld](https://forum.juce.com/u/EmmanWorld)\
**Post date:** [November 5, 2023, 9:33pm UTC](https://forum.juce.com/t/drawing-an-image/58668/7 "2023-11-05T21:33:34Z")

</div>

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_);

```

---

<div class="post-metadata">

**Author:** ![daniel](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/daniel/32/790_2.png) [@daniel](https://forum.juce.com/u/daniel)\
**Post date:** [November 5, 2023, 10:08pm UTC](https://forum.juce.com/t/drawing-an-image/58668/8 "2023-11-05T22:08:40Z")

</div>

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

So does the snippet you posted work for you?

---

<div class="post-metadata">

**Author:** ![EmmanWorld](https://avatars.discourse-cdn.com/v4/letter/e/d07c76/32.png) [@EmmanWorld](https://forum.juce.com/u/EmmanWorld)\
**Post date:** [November 5, 2023, 10:32pm UTC](https://forum.juce.com/t/drawing-an-image/58668/9 "2023-11-05T22:32:16Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![daniel](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/daniel/32/790_2.png) [@daniel](https://forum.juce.com/u/daniel)\
**Post date:** [November 5, 2023, 10:43pm UTC](https://forum.juce.com/t/drawing-an-image/58668/10 "2023-11-05T22:43:02Z")

</div>

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

```auto
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()](https://docs.juce.com/master/classGraphics.html#a5afd17ef7993f8ec1e6ce4ecf721bbff)

---

<div class="post-metadata">

**Author:** ![EmmanWorld](https://avatars.discourse-cdn.com/v4/letter/e/d07c76/32.png) [@EmmanWorld](https://forum.juce.com/u/EmmanWorld)\
**Post date:** [November 6, 2023, 3:45am UTC](https://forum.juce.com/t/drawing-an-image/58668/11 "2023-11-06T03:45:49Z")

</div>

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