Home > Enterprise >  TImage does not resize
TImage does not resize

Time:09-17

I have a TImage on a resizable Form, and I want it to resize when I resize the Form.

What I tried:

  • enabling all Anchors options
  • putting the TImage on a TPanel, and then setting both Panel and Image alignments to alClient
  • Assigning new Width and Height in the Form's OnResize event

None of these does the job.

This is the default Form size. The TImage gets redrawn whenever any of the parameters on the left change, or if the image is zoomed in/out (using the mouse scroll wheel). On each redraw, the pattern repeats until it fills the full TImage.

image

And this is the Form after resizing. Notice that the image size remains unchanged.

image

Enabling the Stretch property does work, but it also scales the image's content, which I don't want.

How can I fix this?

I'm on Delphi 10.3

CodePudding user response:

Rereading your description, I see the problem.

When image dimensions change, inner picture doesn't change its size, if stretching is off. You have to modify inner bitmap size by hands:

Image2.Picture.Bitmap.Width := Image2.Width;
Image2.Picture.Bitmap.Height := Image2.Height;
//now diagonal is drawn properly
Image2.Canvas.MoveTo(0, 0);
Image2.Canvas.LineTo(Image2.Width, Image2.Height);
  • Related