如何等比例缩放图像[1]

[入库:2005年8月18日] [更新:2007年3月25日]

本文简介:选择自 jz_x 的 blog

 

如何等比例缩放图像

作者:徐景周

 

在放大或缩小图像时,如何能最好的保持图像原态比例来显示呢?下面提供一个涵数可帮你来实现,调用它后,返回的矩形区域既为最佳显示图像大小。

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,

			  
本文关键:如何等比例缩放图像
 

本站最佳浏览方式为 分辨率 1024x768 IE 6.0(或更高版本的 IE浏览器)

go top