CBitmap, Hbitmap, bitmap convert each other

Source: Internet
Author: User



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


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.