MFC中CBitmap的簡單複製方法 (Copy CBitmap)

在這裏爲大家提供一種CBitmap複製的方法

經過自己的一層封裝,就形成的非常好用的CBitmap的複製工具函數

先看函數實現:

 

href="http://www.j2megame.org/wupei/plugins/plogeshi/styles/plogeshi.css" type="text/css" rel="stylesheet" />

 

  1. HBITMAP CMyDialog::CopyBitmap(HBITMAP hSourceHbitmap)
  2. {
  3.         CDC sourceDC;
  4.         CDC destDC;
  5.         sourceDC.CreateCompatibleDC(NULL);
  6.         destDC.CreateCompatibleDC(NULL);
  7.         //The bitmap information.
  8.         BITMAP bm = {0};
  9.         //Get the bitmap information.
  10.         ::GetObject(hSourceHbitmap, sizeof(bm), &bm);
  11.         // Create a bitmap to hold the result
  12.         HBITMAP hbmResult = ::CreateCompatibleBitmap(CClientDC(NULL), bm.bmWidth, bm.bmHeight);
  13.  
  14.         HBITMAP hbmOldSource = (HBITMAP)::SelectObject(sourceDC.m_hDC, hSourceHbitmap);
  15.         HBITMAP hbmOldDest = (HBITMAP)::SelectObject(destDC.m_hDC, hbmResult);
  16.         destDC.BitBlt(0, 0, bm.bmWidth, bm.bmHeight, &sourceDC, 0, 0, SRCCOPY );
  17.  
  18.         // Restore DCs
  19.         ::SelectObject(sourceDC.m_hDC, hbmOldSource);
  20.         ::SelectObject(destDC.m_hDC, hbmOldDest);
  21.         ::DeleteObject(sourceDC.m_hDC);
  22.         ::DeleteObject(destDC.m_hDC);
  23.  
  24.         return hbmResult;
  25. }

 

接下來函數調用:

 

  1. //這樣簡單的操作就可以實現CBitmap的複製
  2. CBitmap CpyBitmap;
  3. //返回值可以檢測是否圖片拷貝成功
  4. CpyBitmap->Attach(CopyBitmap(SrcBitmap));

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章