Scenario:
1. When you need to take part of the image area as the background of a widget.
2. You need to tile the image to a large area so that it can be automatically zoomed in.
3. or you need to merge images.
Code:
CDC sdc; CDC ddc;sdc.CreateCompatibleDC(NULL);ddc.CreateCompatibleDC(NULL);CBitmap destBmp;destBmp.CreateCompatibleBitmap(CClientDC(NULL),width,height);sdc.SelectBitmap(m_Bitmap);ddc.SelectBitmap(destBmp);ddc.BitBlt(0, 0, width, height, sdc, rect.left, rect.top, SRCCOPY );
Note: When destbmp calls createcompatiblebitmap, the first parameter passed in is cclientdc. Otherwise, the new cbitmap will not be what you want.
The following section describes how to copy hbitmap objects using the mfc api. Some codes are from the Internet.
Copy the hbitmap object.
static HBITMAP CopyBitmap(HBITMAP hSourceHbitmap,long width,long height,int srcx,int srcy){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 resultHBITMAP hbmResult = ::CreateCompatibleBitmap(CClientDC(NULL), width, height);HBITMAP hbmOldSource = (HBITMAP)::SelectObject( sourceDC.m_hDC, hSourceHbitmap);HBITMAP hbmOldDest = (HBITMAP)::SelectObject( destDC.m_hDC, hbmResult );destDC.BitBlt(0,0,width, height, sourceDC, srcx, srcy, SRCCOPY );// Restore DCs::SelectObject( sourceDC.m_hDC, hbmOldSource );::SelectObject( destDC.m_hDC, hbmOldDest );::DeleteObject(sourceDC.m_hDC);::DeleteObject(destDC.m_hDC);return hbmResult;}
[ATL/wtl] _ [cbitmap copying images-capturing images-tiled images]