Tony The Lion
Tony The Lion

Reputation: 63200

Create a Bitmap (.NET) from HBITMAP

I'm using C++/CLI and I have a raw HBITMAP called hStrip, I have to display this in a PictureBox, so I found the following function System::Drawing::Image::FromHbitmap, however this takes IntPtr and I wondered if I need to convert my raw HBITMAP to IntPtr before passing it in or if I could get away passing it in like this?

If I need to convert, how should I convert? I haven't really found the conversion this way. I did find it the other way though.

Upvotes: 1

Views: 3841

Answers (1)

Hans Passant
Hans Passant

Reputation: 941465

A simple cast gets the job done:

HBITMAP hBmp = NULL;
// Assign hBmp
//...
Bitmap^ bmp = Bitmap::FromHbitmap((IntPtr)hBmp);
DeleteObject(hBmp);  // okay to destroy now

Upvotes: 4

Related Questions