MFC中CBitmap的简单复制方法 (Copy CBitmap)

作者:Wupei  |  发表时间:  |  所属分类:VC

在这里为大家提供一种CBitmap复制的方法

经过自己的一层封装,就形成的非常好用的CBitmap的复制工具函数

先看函数实现:

 

HBITMAP CMyDialog::CopyBitmap(HBITMAP hSourceHbitmap)
{
	CDC sourceDC; 
	CDC destDC; 
	sourceDC.CreateCompatibleDC(NULL); 
	destDC.CreateCompatibleDC(NULL); 
	//The bitmap information. 
	BITMAP bm = {0}; 
	//Get the bitmap information. 
	::GetObject(hSourceHbitmap, sizeof(bm), &bm); 
	// Create a bitmap to hold the result 
	HBITMAP hbmResult = ::CreateCompatibleBitmap(CClientDC(NULL), bm.bmWidth, bm.bmHeight); 

	HBITMAP hbmOldSource = (HBITMAP)::SelectObject(sourceDC.m_hDC, hSourceHbitmap); 
	HBITMAP hbmOldDest = (HBITMAP)::SelectObject(destDC.m_hDC, hbmResult); 
	destDC.BitBlt(0, 0, bm.bmWidth, bm.bmHeight, &sourceDC, 0, 0, SRCCOPY ); 

	// Restore DCs 
	::SelectObject(sourceDC.m_hDC, hbmOldSource); 
	::SelectObject(destDC.m_hDC, hbmOldDest); 
	::DeleteObject(sourceDC.m_hDC); 
	::DeleteObject(destDC.m_hDC); 

	return hbmResult; 
}

接下来函数调用:

//这样简单的操作就可以实现CBitmap的复制
CBitmap CpyBitmap;
//返回值可以检测是否图片拷贝成功
CpyBitmap->Attach(CopyBitmap(SrcBitmap));

Trackback from your site.

请在这里留言: