如何等比例缩放图像
作者:徐景周
在放大或缩小图像时,如何能最好的保持图像原态比例来显示呢?下面提供一个涵数可帮你来实现,调用它后,返回的矩形区域既为最佳显示图像大小。
l 涵数中的第一个参数rcscreen为图像要被显示的矩形大小,第二个参数sizepicture为图像自身大小,第三个参数bcenter为是否居中显示,返回值crect既为图像最佳显示大小。
涵数如下所示:
crect rectsizewithconstantratio( crect* rcscreen, csize sizepicture, bool bcenter){ crect rect(rcscreen); double dwidth = rcscreen->width(); double dheight = rcscreen->height(); double daspectratio = dwidth/dheight; double dpicturewidth = sizepicture.cx; double dpictureheight = sizepicture.cy; double dpictureaspectratio = dpicturewidth/dpictureheight; //if the aspect ratios are the same then the screen rectangle // will do, otherwise we need to calculate the new rectangle if (dpictureaspectratio > daspectratio) { int nnewheight = (int)(dwidth/dpicturewidth*dpictureheight); int ncenteringfactor = (rcscreen->height() - nnewheight) / 2; rect.setrect( 0, ncenteringfactor,