Scaling image to fit the bounds
Brought to you by:
ddiego
Within the GraphicsKit, there's a method
drawImageWithinBounds, which really just clips the
image to the bounds. What I need is to actually scale it
(whether that means stretching or shrinking) so that the
entire image fits exactly to those bounds. Maybe this
could be a new method - say drawImageToBounds.
It would of course need to account for any scaling
currently in affect as well. For example:
ctx->setScale(2,2);
ctx->drawImageToBounds(&Rect(10,20,100,150),
imgTest);
This should draw imgTest at point (5,10) with a size of
45x65.
Logged In: YES
user_id=1206567
Here's some code for a local method I wrote to do this for
me - if you like the idea, you can move it into the GraphicsKit.
void drawImageToBounds(GraphicsContext* ctx, Rect*
rBound, Image* imgDraw)
{
// Get the current scaling in affect
double dblOrigX = ctx->getScaleX();
double dblOrigY = ctx->getScaleY();
// Get the desired change for scaling the image
double dblChangeX = rBound->getWidth() / imgDraw-
>getWidth();
double dblChangeY = rBound->getHeight() /
imgDraw->getHeight();
// Get the reverse-scaled point to draw at
Point pDraw = rBound->getTopLeft() / Point
(dblChangeX, dblChangeY);
// Get the new scale to apply
double dblNewX = dblOrigX * dblChangeX;
double dblNewY = dblOrigY * dblChangeY;
// Temp - have to adjust X scaling until bug
1110901 is fixed
dblNewX *= 1.33333;
pDraw.x_ /= 1.33333;
// Do the actual scale and draw
ctx->setScale(dblNewX, dblNewY);
ctx->drawImage(pDraw, imgDraw);
ctx->setScale(dblOrigX, dblOrigY);
}