在這裏爲大家提供一種CBitmap複製的方法
經過自己的一層封裝,就形成的非常好用的CBitmap的複製工具函數
先看函數實現:
href="http://www.j2megame.org/wupei/plugins/plogeshi/styles/plogeshi.css" type="text/css" rel="stylesheet" />
-
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));