Search code examples
c++qtgdi+qimageqpixmap

Convert GDI Plus Bitmap to QPixmap


I have a GdiPlus::Bitmap object, and I need to build a function that converts it to a QPixmap. I'm using Qt 5.8.0, and performance is a concern of mine. I cannot find anything about this online, even searching for GDI+ images to QImages. I don't NEED to access pixel data, so all I have to do is get a GDI+ bitmap converted into something that can be displayed in a QWidget.

The most promising solution I've found so far is to use the fromHBitMap() in QtWin: http://doc.qt.io/qt-5/qtwin.html#fromHBITMAP, but I don't have the required knowledge/experience to be able to understand handles to bitmaps.


Solution

  • I assume you have "Bitmap" object in your hand. Also I assume your development environment supports both "Windows programming" and "Qt".

    Based on above assumptions, you can get the HICON from the "Bitmap" and pass it to "QPixmap QtWin::fromHICON(HICON icon)".

    Steps:

    First you have to include "Gdiplus.h". Also include "QtWin".

    HICON hIcon;
    Status st = <<Your Bitmap Object>>.GetHICON(&hIcon);
    QPixmap pixM = QtWin::fromHICON(hIcon);
    

    Try this. The above code is untested. It gives an idea.