图片等比例填充rect
typedef struct Pos{
int top;
int left;
int width;
int height;
}MyPos;
newWidth,newHeight是适配后的结果,border:预留边框大小默认可为0
void CShuZiShiBieDlg::AdjustImage2Rect(CRect& rect, CImage& image, MyPos& pos, int borderSize)
{
float w = image.GetWidth(); //图像本身大小
float h = image.GetHeight();
float ww = rect.Width();
float hh = rect.Height();
// rect>>pos 预留一像素边框
pos.left = rect.left + borderSize;
pos.top = rect.top + borderSize;
pos.width = ww - 2 * borderSize;
pos.height = hh - 2 * borderSize;
//缩放比。使用长宽固定比例缩放>>只用高或者宽其中一个数据进行计算即可
float beW = 1, beH = 1, beRes = 1;//be=1表示不缩放
beW = w / ww, beH = h / hh;
int left = 0, top = 0;
//缩放,扩张
if (beW>beH){
pos.height = (int)(h / beW) - 2 * borderSize;
pos.top = (hh - pos.height) / 2 + borderSize;
}
else if (beW<beH)
{
pos.width = (int)(w / beH) - 2 * borderSize;
pos.left = (ww - pos.width) / 2 + borderSize;
}
}