Search code examples
c++-clitype-conversionintptrhbitmap

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.


Solution

  • A simple cast gets the job done:

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