7

In my windows-only program, I use a third-party library, which returns a HBITMAP.

Is there a way to initialize a QImage from its contents, i.e. to convert it to a QImage?

László Papp
  • 51,870
  • 39
  • 111
  • 135
sashoalm
  • 75,001
  • 122
  • 434
  • 781

3 Answers3

11

This is the way to do it for Qt 4 (QtGui):

QImage image(QPixmap::fromWinHBITMAP(hBitmap).toImage());

This is the way to do it for Qt 5 (QtWinExtras):

QPixmap pixmap = QtWin::fromHBITMAP(hBitmap);
QImage image = pixmap.toImage();

// or

QtWin::imageFromHBITMAP(hdc, hBitmap, width, height)
László Papp
  • 51,870
  • 39
  • 111
  • 135
3

OK, this seems to work for me:

QImage image(QPixmap::fromWinHBITMAP(hBitmap).toImage());
sashoalm
  • 75,001
  • 122
  • 434
  • 781
1

Qt5 without Extras: Put before your code

#include <QPixmap>
Q_GUI_EXPORT QPixmap qt_pixmapFromWinHBITMAP(HBITMAP bitmap, int hbitmapFormat=0);

and in your function, for example

QPixmap pixmap = qt_pixmapFromWinHBITMAP(LoadBitmap(uiID));

Cheers

user3619296
  • 1,607
  • 1
  • 11
  • 3