天天看點

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

在這裡為大家提供一種CBitmap複制的方法

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

先看函數實作:

href="http://www.j2megame.org/wupei/plugins/plogeshi/styles/plogeshi.css" target="_blank" rel="external nofollow" 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.         HBITMAP hbmOldSource = (HBITMAP ):: SelectObject (sourceDC. m_hDC, hSourceHbitmap );
  14.         HBITMAP hbmOldDest = (HBITMAP ):: SelectObject (destDC. m_hDC, hbmResult );
  15.         destDC. BitBlt ( 0, 0, bm. bmWidth, bm. bmHeight, &sourceDC, 0, 0, SRCCOPY );
  16.         // Restore DCs
  17.         :: SelectObject (sourceDC. m_hDC, hbmOldSource );
  18.         :: SelectObject (destDC. m_hDC, hbmOldDest );
  19.         :: DeleteObject (sourceDC. m_hDC );
  20.         :: DeleteObject (destDC. m_hDC );
  21.         return hbmResult;
  22. }

接下來函數調用:

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

繼續閱讀