Board index » cppbuilder » TImage scaling problem - drawing on stretched image

TImage scaling problem - drawing on stretched image


2005-03-05 01:23:16 AM
cppbuilder30
If I have Stretch set to true, I have no problem, but if I load an image
with Stretch set to true when the circle is drawn (in the TImage's
OnMouseMove, using the provided X,Y coords) the pos is offset from the
mouse cursor.
// In the Timage's OnMouseMove
Image1->Canvas->Pen->Color=Color1; // Color1/2 set else where to
Image1->Canvas->Brush->Color=Color2; // desired colors.
Image1->Canvas->Ellipse(X-4, Y-4, X+4, Y+4);
I also had the same problem when manually drawing a border, from 0,0 to
the TImage's Width,Height, without Stretch it would draw all the way
around, with it set to true the border is drawn from 0,0 to only part
way (around half I think) of the width and height, looking like a box
outline inside the image (smaller than the image) with the Left and Top
sides at 0,0.
// This executes when the program loads and rigth after an image is
loaded
Image1->Canvas->Brush->Color = clBlack;
Image1->Canvas->FrameRect(Rect(0,0, Image1->Width, Image1->Height));
This seems to be a scaling problem, but how cna I convert the X,Y, and
WidthmHeight values to what they physicially are on screen so i can draw
on the canvas correctly?
Thank you for any help.
 
 

Re:TImage scaling problem - drawing on stretched image

Hi Segal,
You can determine the scaling factors by taking the ratio of the
TImage's dimensions to the dimensions of the TBitmap that the TImage
contains...
float const scaleX = Image1->Width /
static_cast<float>(Image1->Picture->Bitmap->Width);
float const scaleY = Image1->Height /
static_cast<float>(Image1->Picture->Bitmap->Height);
X /= scaleX;
Y /= scaleY;
// In the Timage's OnMouseMove
Image1->Canvas->Pen->Color=Color1;
Image1->Canvas->Brush->Color=Color2;
Image1->Canvas->Ellipse(X-4, Y-4, X+4, Y+4);
For reference, when you draw to a TImage's Canvas, you're actually
drawing to the canvas associated with the bitmap that the image
contains. When the image's Stretch property is set to true, because the
bitmap is drawn in a scaled-to-fit fashion [via the StretchBlt() GDI
function], anything that you draw will also be drawn scaled-to-fit.
This is the reason that you need to translate your coordinates--you're
drawing to a bitmap, not to the screen.
HTH,
Segal wrote:
Quote
If I have Stretch set to true, I have no problem, but if I load an image
with Stretch set to true when the circle is drawn (in the TImage's
OnMouseMove, using the provided X,Y coords) the pos is offset from the
mouse cursor.

// In the Timage's OnMouseMove
Image1->Canvas->Pen->Color=Color1; // Color1/2 set else where to
Image1->Canvas->Brush->Color=Color2; // desired colors.
Image1->Canvas->Ellipse(X-4, Y-4, X+4, Y+4);

This seems to be a scaling problem, but how cna I convert the X,Y, and
WidthmHeight values to what they physicially are on screen so i can draw
on the canvas correctly?