求帮助,修改一下图片等比例缩放的代码

void_main_void 2016-03-11 05:35:18
这是我自己改的图片等比例缩放的代码,如果碰到长方形的图片就没法等比例缩放了。。这里代码改的有问题么。

如果我想让图片在180X180正方形大小内等比例缩放显示,我构造的参数一为:QRect rcScreen(0, 0, 180, 180)
这样子有问题么。。楼主数渣。轻喷。。


//参数一:需要等比例缩放的矩形
//参数二:图片大小
//返回值,为转换后的矩形
//如果类似正方形,的都能正常显示
//如果是长方形那么久没法等比例缩放了。。。。
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;
}


这个是Qt下错误的变形效果

这个是MFC正确效果



//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;
}
...全文
224 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
void_main_void 2016-03-12
  • 打赏
  • 举报
回复
引用 3 楼 u014724231 的回复:
缩放是使用QPalette的setBrush实现的吗?

QSize imageUpSize = imageZoom(
        QSize(imageSize, imageSize),
        QSize(m_imagePixmap.width(), m_imagePixmap.height()));

    m_imagePixmap = m_imagePixmap.scaled(imageUpSize, Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
暴暴熊 2016-03-11
  • 打赏
  • 举报
回复
缩放是使用QPalette的setBrush实现的吗?
gfvera 2016-03-11
  • 打赏
  • 举报
回复
等会看看有没有人来收分的。。没有就小号接分了。。。。
void_main_void 2016-03-11
  • 打赏
  • 举报
回复
好了。。。。nCenteringFactor 这个变量不要加就好。。。

16,203

社区成员

发帖
与我相关
我的任务
社区描述
Qt 是一个跨平台应用程序框架。通过使用 Qt,您可以一次性开发应用程序和用户界面,然后将其部署到多个桌面和嵌入式操作系统,而无需重复编写源代码。
社区管理员
  • Qt
  • 亭台六七座
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧