返回> 网站首页 

HBITMAP的复制,拷贝 与 CBitmap的复制,拷贝

yoours2011-03-09 10:25:23 阅读 1421

简介一边听听音乐,一边写写文章。


HBITMAP  Cxxx::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;
}

========================================================
在网上大部分都是说用临时CDC,Bitblt来拷贝
这个方法简单点,就是先把CBitmap强制转换到HBITMAP后再Attach()

void Cxxx::CopyBitmap(CBitmap* hSourceBitmap, CBitmap* hDescBitmap)
{
HBITMAP HBM=(HBITMAP)hSourceBitmap->m_hObject;

//HBITMAP HBM=(HBITMAP)hSourceBitmap->Detach();//如果希望清除掉原图资源
//hDescBitmap = new CBitmap;
hDescBitmap->Attach(HBM);
}

微信小程序扫码登陆

文章评论

1421人参与,0条评论