LikeYou
LikeYou

Reputation: 500

MFC 7.0 C++ How to get a CBitmap from CImage

I like to know how to get a CBitmap from CImage in MFC code.

The problem is MSDN tells me to use CImage::m_hBitmap to generate a CBitmap from handle. But unfortunately CImage::m_hBitmap is private to CImage. Do I get something wrong?

Code excerpt from MSDN:

CImage image;
// Code to create load/create image goes here
// ...
CBitmap* pBitmap = CBitmap::FromHandle(image.m_hBitmap);

Thanks!

Upvotes: 3

Views: 6425

Answers (1)

LikeYou
LikeYou

Reputation: 500

To get the handle from m_hBitmap use the CImage object directly. The online help at MSDN seems not updated correctly.

The MFC developer(s) has added an operator HBITMAP, so conversion from CImage to HBITMAP works without user intervention.

The inline code that gives access to m_hBitmap in header atlimage.h:

inline CImage::operator HBITMAP() const throw()
{
  return (m_hBitmap);
}

In order to have a working code image.m_hBitmap must be replaced with image only.

CImage image;
// Code to create load/create image goes here
// ...
CBitmap* pBitmap = CBitmap::FromHandle(image);

That's it.

EDIT: I've found an alternative solution too, see here.

Upvotes: 5

Related Questions