Search code examples
delphidrawingscale

Resize image distorts mouse X/Y


I have a VCL Form that has a TImage in it with anchors set for all sides so when the user resizes the form the image size matches the new size (almost to fill, there are some buttons).

However the drawing tools I have for the image's canvas get skewed when I resize. The drawing works fine before I resize, then after, I seem to be drawing at some other spot then where my mouse is clicking.

How can I resize a TImage and reference the new image's X/Y so the drawing comes out correctly?

MouseDown:

Drawing := true;

StartX := x;
StartY := y;

EndX := x;
EndY := y;

MouseMove:

if Drawing then begin

   Image1.Canvas.Pen.Mode := pmNotXor;

   Image1.Canvas.MoveTo(StartX,StartY);
   Image1.Canvas.LineTo(EndX,EndY);
   Image1.Canvas.MoveTo(StartX,StartY);
   Image1.Canvas.LineTo(x,y);

end;

EndX := x;
EndY := y;

Image1.Canvas.Pen.Mode := pmCopy;

MouseUp:

Image1.Canvas.MoveTo(StartX,StartY);
Image1.Canvas.LineTo(x,y);
Drawing := false;

Solution

  • I guess the Strech property of the image is set to true. In that case the bitmap created from the image stays with its former size.

    When you access Image1.Canvas for the first time a TBitmap is created with the size of Image1. This bitmap size is not adjusted when image1 is resized. The Strech = true renders the bitmap to the current image size, but the bitmap itself is the former size.

    You can fix this by adjusting Image1.Picture.Bitmap whenever Image1 is resized. As TImage has no OnResize you can use a TPanel as a container and make the adjustment inn its OnResize event.