下面代码没有参数,因为两个指定图片的路径都是直接写进去的,你也可以选择传进去,小修改下
/*
函 数 名:
功 能:将图片缩小
参 数:
返 回 值:
调用位置:
注意事项:
*/
int ZoomBmp()
{
CImage m_jpgImage;
if(!m_jpgImage.IsNull())
m_jpgImage.Destroy();
HRESULT hResult=m_jpgImage.Load(_T("D:\\11.bmp")); //要缩小图片的路径
if(hResult != S_OK)
return FALSE;
int iWidth=m_jpgImage.GetWidth();
int iHeight=m_jpgImage.GetHeight();
RECT srcrect, desrect;
srcrect.left = 0;
srcrect.top = 0;
srcrect.right = iWidth;
srcrect.bottom = iHeight;
desrect.left = 0;
desrect.top = 0;
desrect.right = 750;//固定宽度
desrect.bottom = 370;//固定高度
CDC dc;
CDC MemDC;
HDC hDc;
hDc = ::GetDC(NULL);
dc.Attach(hDc);
MemDC.CreateCompatibleDC(&dc);//创建C兼容的内存DC
CBitmap hmap;
hmap.CreateCompatibleBitmap(&dc,desrect.right,desrect.bottom);
MemDC.SelectObject(hmap);
// 開始縮圖 //
float scale_w = (float)iWidth/(float)desrect.right;
float scale_h = (float)iHeight/(float)desrect.bottom;
//scale_h > scale_w ? scale_h = scale_w : scale_w = scale_h;
for (int x=0; x<desrect.right; x++)
{
for (int y=0; y<desrect.bottom; y++)
{
unsigned long rsum=0, gsum=0, bsum=0, count=0;
int sx = (int)(x*scale_w);
int sy = (int)(y*scale_h);
for (int i=0; i<scale_w; i++)
for (int j=0; j<scale_h; j++)
{
COLORREF cr = m_jpgImage.GetPixel(sx+i,sy+j);
rsum += GetRValue(cr);
gsum += GetGValue(cr);
bsum += GetBValue(cr);
count++;
}
int r = rsum/count;
int g = gsum/count;
int b = bsum/count;
MemDC.SetPixel(x,y,RGB(r,g,b));
}
}
dc.Detach();
// 將縮圖存檔 //
CImage CCPic;
CCPic.Attach(hmap,CImage::DIBOR_DEFAULT);
CCPic.Save(_T("D:\\11.bmp"));//缩小后要存放的路径
ReleaseDC(NULL, hDc);
return TRUE;
}