实现图片在控件中的按比例缩放方法
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;
}
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;
}
相关文章推荐
- JS实现按比例缩放图片的方法(附C#版代码)
- php实现等比例不失真缩放上传图片的方法
- PHP自定义图片缩放函数实现等比例不失真缩放的方法
- Android 使用Picasso加载网络图片等比例缩放的实现方法
- Android 使用Glide加载网络图片等比例缩放的实现方法
- JS实现控制图片显示大小的方法【图片等比例缩放功能】
- VB--图片在PICETURE控件里等比例缩放的方法
- Android开发之imageView图片按比例缩放的实现方法
- 网页图片按比例自适应缩放实现方法
- JS实现按比例缩放图片的方法(附C#版代码)
- JS网页图片按比例自适应缩放实现方法
- jQuery实现按比例缩放图片的方法
- PHP自定义图片缩放函数实现等比例不失真缩放的方法
- JS网页图片按比例自适应缩放实现方法
- S实现控制图片显示大小的方法【图片等比例缩放功能】
- php实现等比例不失真缩放上传图片的方法
- JS实现按比例缩放图片的方法(附C#版代码)
- 按比例缩放图片的两种方法:JS&CSS
- 用 JavaScript 实现网页图片等比例缩放
- asp.net用鼠标滚轮实现图片缩放实现方法