您的位置:首页 > 其它

实现图片在控件中的按比例缩放方法

2014-06-16 16:35 555 查看
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,

(int)dWidth,

nNewHeight + nCenteringFactor);

}

else if (dPictureAspectRatio < dAspectRatio)

{

int nNewWidth = (int)(dHeight/dPictureHeight*dPictureWidth);

int nCenteringFactor = (rcScreen->Width() - nNewWidth) / 2;

rect.SetRect( nCenteringFactor,

0,

nNewWidth + nCenteringFactor,

(int)(dHeight));

}

return rect;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: