16,203
社区成员
发帖
与我相关
我的任务
分享
//参数一:需要等比例缩放的矩形
//参数二:图片大小
//返回值,为转换后的矩形
//如果类似正方形,的都能正常显示
//如果是长方形那么久没法等比例缩放了。。。。
QRect CImageItem::imageZoom(QRect rcScreen, QSize sizePicture)
{
QRect rect(rcScreen);
double dWidth = rcScreen.width();
double dHeight = rcScreen.height();
double dAspectRatio = dWidth / dHeight;
double dPictureWidth = sizePicture.width();
double dPictureHeight = sizePicture.height();
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);
//将参数一和返回值修改为 QSize 还是一样的无效。。。。
//rect.setWidth((int)dWidth);
//rect.setHeight(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));
//将参数一和返回值修改为 QSize 还是一样的无效。。。。
//rect.setWidth(nNewWidth + nCenteringFactor);
//rect.setHeight((int)(dHeight));
}
return rect;
}
//1. 这是MFC 的图片等比例缩放。 求转换成Qt的。
static 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;
}
QSize imageUpSize = imageZoom(
QSize(imageSize, imageSize),
QSize(m_imagePixmap.width(), m_imagePixmap.height()));
m_imagePixmap = m_imagePixmap.scaled(imageUpSize, Qt::IgnoreAspectRatio, Qt::SmoothTransformation);