Delphi – How to get the current view size for an image in Delphi 2009

delphisizetimage

I wish to get the current size for an image being displayed with TImage, this object has the Proportional property set to True and is aligned to Client with main form. So, when resizing the form I wanna get the current size for that image, not canvas size neither real size for that image. Getting the current size I can show what's the percentage for current image size respect to real image size.
Thanks in advance

Best Answer

You can compare Image1.Picture.Width or .Height with Image1.Width or .Height.

To know if the image is streched proportional using the horizontal or vertical dimension, you should compare the ratio's of the two:

if Image1.Width/Image1.Height > Image1.Picture.Width/Image1.Picture.Height then 
  Result:=Image1.Picture.Width/Image1.Width
else
  Result:=Image1.Picture.Height/Image1.Height;

using a mathematical trick to convert the divisions into multiplications, you can avoid the conversions into float values, which also calculate a bit faster by using:

if Image1.Width*Image1.Picture.Height >Image1.Picture.Width*Image1.Height then
Related Topic