MFC: Convert CImage to HBITMAP

How to convert from CImage to HBITMAP?

CImage image;
HRESULT sc=image.Load(stringload);
HBITMAP hbmp=image.Detach();

This doesn't work! Any solution to this is very welcomed!

The opposite works very well:

CImage image;
HRESULT sc=image.Save(stringsave);
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Just use 'CImage::operator HBITMAP()'  ( to do that, i.e.
CImage image;
HRESULT sc = image.Load(stringload);
HBITMAP hbmp = (HBITMAP) image;

Open in new window

buddirdAuthor Commented:
Thank you! I have also seen this, but it won't help me.

Your suggestion can be compiled, but when I run it I get out of memory when trying to execute:

sc = ::OleCreateStaticFromData(this, IID_IOleObject, OLERENDER_FORMAT, &m_fromat, pOleClientSite, pStorage, (void **)&pOleObject);

When I use HBITMAP, which I got from the clipboard the same works perfectly. Every time.

What is the difference between CImage HBITMAP and HBITMAP from clipboard? Any work around?

Hm, I cannot see any HBITMAP in the above line...
Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

buddirdAuthor Commented:
You are right! It is the line of code which fails because I have another HBITMAP than from the clipboard. In short I am using CImageDataObject by Hani Atassi. It is a class for inserting HBITMAP into CRichEditCtrl. ("Interface to be used in inserting bitmap to the RichEdit Control.")

As input to the class I have HBITMAP and a pointer to IRichEditOle. When I use HBITMAP from the clipboard everything works every time, but never with HBITMAP from CImage.

What I am going to do is to insert an image from disk into the CRichEditCtrl.

That's why I need your help. Thank you in advance.
Well, take a look at ("A Rich Edit Control That Displays Bitmaps and Other OLE Objects") on how to display stuff in such a control

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
buddirdAuthor Commented:
Thank you! That was what I needed. You get excellent for this:)
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Development

From novice to tech pro — start learning today.