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

霸气de小男生 提交于 2019-12-23 16:53:38

问题


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


回答1:


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



回答2:


I don't think there's a built-in way to do this. You may just have to calculate it. Something like this:

  1. Get the height and width of the canvas.
  2. Get the height and width of the image's Picture property.
  3. Calculate the ratios of the Picture's dimensions to the canvas's dimensions.
  4. Whichever of the two ratios is smaller is your percentage.



回答3:


Load this picture into a virtual TCanvas (or whatever) and then leave the property Proportional as False. Now you can retrieve the original size of the picture. If the image is usually very big, maybe you should use some external graphical libraries.



来源:https://stackoverflow.com/questions/1262023/how-to-get-the-current-view-size-for-an-image-in-delphi-2009

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!