在設置用戶頭像的時候,從網絡上下載下來的用戶頭像的尺寸可能和我們本身想要的頭像的尺寸不一致,這時候我們就需要對用戶頭像的尺寸進行修改,下面直接給出代碼,需要的朋友可以參考。
//改變圖片的尺寸
bool ResizePicture(CString strSource, CString strTarget)
{
int WIDTH = 70;
int HEIGHT = 70;
CImage oldimg;
CImage newimg;
oldimg.Load(strSource);
if (oldimg.IsNull())
return false;
int nWidth = 0;
int nHeight = 0;
nWidth = oldimg.GetWidth();
nHeight = oldimg.GetHeight();
if (nWidth > WIDTH || nHeight > HEIGHT)
{
double dRatio = nWidth * 1.0 / nHeight;
if (nWidth > nHeight)
{
nWidth = WIDTH;
nHeight = (int)(nWidth / dRatio);
}
else
{
nHeight = HEIGHT;
nWidth = (int)(nHeight * dRatio);
}
}
if (!newimg.CreateEx(nWidth, nHeight, 24, BI_RGB))
{
oldimg.Destroy();
return false;
}
int nPreMode = ::SetStretchBltMode(newimg.GetDC(), HALFTONE);
newimg.ReleaseDC();
oldimg.Draw(newimg.GetDC(), 0, 0, nWidth, nHeight, 0, 0, oldimg.GetWidth(), oldimg.GetHeight());
newimg.ReleaseDC();
::SetBrushOrgEx(newimg.GetDC(), 0, 0, NULL);
newimg.ReleaseDC();
::SetStretchBltMode(newimg.GetDC(), nPreMode);
newimg.ReleaseDC();
newimg.Save(strTarget);
newimg.Destroy();
oldimg.Destroy();
return true;
}