我如何计算缩放级别(图形比例)以使任何图像适合任何面板?
图片大小和图片大小可以是任意大小。
我需要的方法签名如下:
public float CalculateZoomToFit(Image image, Panel targetPanel)
{
// I need to calculate the zoom level to make the picture fit into the panel
return ???
}
提前致谢。
最佳答案
面板和图像的宽高比是答案的关键。
var panel_ratio = targetPanel.Width / targetPanel.Height;
var image_ratio = image.Width / image.Height;
return panel_ratio > image_ratio
? targetPanel.Height / image.Height
: targetPanel.Width / image.Width
;
如果需要,添加对被零除错误的检查。
关于c# - 计算缩放级别以使图像适合面板,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9366548/