求帮助,修改一下图片等比例缩放的代码
这是我自己改的图片等比例缩放的代码,如果碰到长方形的图片就没法等比例缩放了。。这里代码改的有问题么。
如果我想让图片在180X180正方形大小内等比例缩放显示,我构造的参数一为:QRect rcScreen(0, 0, 180, 180)
这样子有问题么。。楼主数渣。轻喷。。
这个是Qt下错误的变形效果

这个是MFC正确效果

如果我想让图片在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;
}
...全文
相关推荐
Python代码批处理图片,按照宽度等比例缩放
Python代码批处理图片,按照宽度等比例缩放 对比PS自带的批处理,效率提高11倍左右 适合电商网店大量商品图处理
Android 使用Glide加载网络图片等比例缩放的实现方法
主要介绍了Android 使用Glide加载网络图片等比例缩放的实现方法,需要的朋友可以参考下
java处理图片大小等比例缩放,自定义修改图片大小,截取,水印
java 处理图片大小等比例缩放;图片裁剪;支持图片压缩不失真;支持图片添加水印;支持动态图的处理;支持自定义图片大小的转换 等 压缩包中内附使用说明
微信小程序 图片等比例缩放(图片自适应屏幕)
主要介绍了微信小程序 图片等比例缩放(图片自适应屏幕)的相关资料,需要的朋友可以参考下
图片等比例缩放代码,图片等比例缩放JS代码,图片缩放代码
图片缩放 等比例缩放 图片等比例缩放 等比例缩放JS代码 JS代码 WEB网页制作。。


