QImage from HBITMAP

☆樱花仙子☆ 提交于 2019-12-10 15:09:06

问题


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?


回答1:


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)



回答2:


OK, this seems to work for me:

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



回答3:


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



来源:https://stackoverflow.com/questions/14568600/qimage-from-hbitmap

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!