MFC指定图片路径,缩小图片,再保存到一个路径

下面代码没有参数,因为两个指定图片的路径都是直接写进去的,你也可以选择传进去,小修改下
/*
函 数 名:
功    能:将图片缩小
参    数:
返 回 值:
调用位置:
注意事项:
*/
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;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值