如果我有一个我知道高度和宽度的图像,那么我怎么能将它放在一个矩形最大的矩形中,而不会拉伸图像.
伪代码是足够的(但我将在Java中使用).
谢谢.
所以,根据答案,我写道:但是不行.我做错了什么
double imageRatio = bi.getHeight() / bi.getWidth();
double rectRatio = getHeight() / getWidth();
if (imageRatio < rectRatio)
{
// based on the widths
double scale = getWidth() / bi.getWidth();
g.drawImage(bi, 0, 0, (int) (bi.getWidth() * scale), (int) (bi.getHeight() * scale), this);
}
if (rectRatio < imageRatio)
{
// based on the height
double scale = getHeight() / bi.getHeight();
g.drawImage(bi, 0, 0 , (int) (bi.getWidth() * scale), (int) (bi.getHeight() * scale), this);
}