简介本文介绍一个C++代码实战例子:C++实现图片等比例缩放,感兴趣的朋友可以参考一下。

C++实现图片等比例缩放

/**
* 图片等比例缩放
*
* @param iImgWidth		图片原始宽度
* @param iImgHeight		图片原始高度
* @param iScreenWidth	显示宽度
* @param iScreenHeight	显示高度
* @param iImgNewWidth	缩放后宽度
* @param iImgNewHeight	缩放后高度
*
**/
void PictureScale(int iImgWidth, int iImgHeight, int iScreenWidth, int iScreenHeight, int& iImgNewWidth, int& iImgNewHeight)
{
	iImgNewWidth = iImgWidth;
	iImgNewHeight = iImgHeight;
	if (iImgWidth > iScreenWidth || iImgHeight > iScreenHeight)
	{
		double dRatioWidth = (double)iScreenWidth / iImgWidth;
		double dRatioHeight = (double)iScreenHeight / iImgHeight;
		double dRatio = dRatioWidth < dRatioHeight ? dRatioWidth : dRatioHeight;
		iImgNewWidth = (int)(iImgWidth * dRatio + 0.5);
		iImgNewHeight = (int)(iImgHeight * dRatio + 0.5);
	}
}


更多为你推荐