【问题标题】:QImage from HBITMAP来自 HBITMAP 的 QImage
【发布时间】:2013-01-28 18:24:41
【问题描述】:

在我的纯 Windows 程序中,我使用第三方库,它返回 HBITMAP

有没有办法从 QImage 的内容中初始化它,即将其转换为 QImage

【问题讨论】:

    标签: c++ qt qimage qtgui hbitmap


    【解决方案1】:

    这是 Qt 4 (QtGui) 的方法:

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

    这是 Qt 5 (QtWinExtras) 的方法:

    QPixmap pixmap = QtWin::fromHBITMAP(hBitmap);
    QImage image = pixmap.toImage();
    
    // or
    
    QtWin::imageFromHBITMAP(hdc, hBitmap, width, height)
    

    【讨论】:

    • 对于 Qt 6,fromWinHBITMAP 现在是 QImage 的一部分,因此您只需调用 QImage.fromHBITMAP
    【解决方案2】:

    好的,这似乎对我有用:

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

    【讨论】:

    • 这在 Qt 4.8 之后不可用
    【解决方案3】:

    没有附加功能的 Qt5: 放在你的代码之前

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

    在你的函数中,例如

    QPixmap pixmap = qt_pixmapFromWinHBITMAP(LoadBitmap(uiID));
    

    干杯

    【讨论】:

    • 这不是公共 API。
    猜你喜欢
    • 2023-03-06
    • 1970-01-01
    • 2018-07-16
    • 1970-01-01
    • 2013-02-24
    • 1970-01-01
    • 1970-01-01
    • 2012-01-18
    • 2023-03-07
    相关资源
    最近更新 更多