First: Understanding
Bitmap is a bitmap structure defined in C + +
Hbitmap is a bitmap handle used in Windows
CBitmap is a bitmap class for MFC encapsulation
Second: Mutual conversion
1, Hbitmap->cbitmap
HBITMAP hBitmap=(HBITMAP)::LoadImage(NULL, str, IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE);
CBitmap bitmap;
bitmap.Attach(hBitmap);
experimental source, added in (OnPaint function)
CString str = _T("E:\\picture\\lena.bmp");
HBITMAP hBitmap=(HBITMAP)::LoadImage(NULL, str, IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE);
CBitmap bitmap;
bitmap.Attach(hBitmap);
CPaintDC dc(this);
CDC MemDC;
MemDC.CreateCompatibleDC(&dc);
MemDC.SelectObject(&bitmap);
CRect rect;
GetClientRect(&rect);
dc.BitBlt(0, 0, rect.Width(), rect.Height(), &MemDC, 0, 0, SRCCOPY);
Note: The member functions in the CBitmap class:
BOOL LoadBitmap (LPCTSTR lpszrecourcename);
BOOL LoadBitmap (UINT nidresource);
can load bitmaps, but they can only load bitmaps in the project, and cannot load bitmaps on the hard disk like LoadImage. In particular, be aware that:
the Lpszrecourcename in the BOOL LoadBitmap (LPCTSTR lpszrecourcename) function cannot be a path string. It refers to a bitmap whose ID is represented by a string.
For example: I created a bitmap resource in the project IDB_BITMAP1, lpszresourcename refers to what is the hard disk bitmap1.bmp, if so, the following code for what is wrong.
CBitmap bmp;
bmp.LoadBitmap("d:\\..\\res\\bitmpa1.bmp");
CDC memdc;
BITMAP bm;
bmp.GetBitmap(&bm);
memdc.CreateCompatibleDC(pDC);
memdc.SelectObject(&bmp);
pDC->BitBlt(0,0,bm.bmWidth,bm.bmHeight,&memdc,0,0,SRCCOPY);
Open the *.rc file with Notepad, and find a line similar to the following:
Idb_bitmap BITMAP "Res\\background.bmp"
Change to: Bitmap1 BITMAP "Res\background.bmp"
Alternatively, look at the properties of the bitmap resource in the VC and change its ID field to "Bitmap" (note, be sure to add quotes).
Then call: BMP. LoadBitmap ("Bitmap1"); Guarantee success.
A resource can be marked with an integer, or it can be marked with a string. However, these IDs are not referred to as bitmap filenames.
2, Hbitmap->bitmap
CString str = _T("E:\\picture\\lena.bmp");
HBITMAP hBitmap = (HBITMAP)::LoadImage(NULL, str, IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE);
BITMAP bm;
::GetObject(hBitmap, sizeof(bm), &bm);
3, Cbitmap->bitmap
CBitmap bitmap;
bitmap.LoadBitmapW(IDB_BITMAP1);
BITMAP bm;
bitmap.GetBitmap(&bm);
4, Cbitmap->hbitmap
CBitmap, Hbitmap, bitmap convert each other