How to convert GDI bitmap to QPixmap
-
wrote on 11 Sept 2022, 00:46 last edited by
How to convert a GDI GpBitmap* to QPixmap?
-
There's no direct conversion, but QImage can be created from HBITMAP, so you can do it in 2 steps: create HBITMAP from GpBitmap and then create QImage out of that. Something like:
Gdiplus::GpBitmap* gpBitmap = /* something */; HBITMAP bitmap {}; Gdiplus::DllExports::GdipCreateHBITMAPFromBitmap(gpBitmap, &bitmap, Gdiplus::Color::Transparent); QImage image = QImage::fromHBITMAP(bitmap); DeleteObject(bitmap);
Note that
QImage::fromHBITMAP
is available in Qt6.
In Qt5 it was part of a separate module QtWinExtras: QtWin::imageFromHBITMAP()It's a bit of a waste because it's doing two conversions. If you care for performance the best would be to get access to the GpBitmap raw data and create QImage from that directly, but that's more work. The above is the lazy way.
-
wrote on 11 Sept 2022, 03:00 last edited by Cesar 9 Nov 2022, 03:02
Does it preserve transparency?
get access to the GpBitmap raw data and create QImage from that directly
Do you mean lockbits?
-
Does it preserve transparency?
get access to the GpBitmap raw data and create QImage from that directly
Do you mean lockbits?
Does it preserve transparency?
I don't know. Check.
Do you mean lockbits?
Yeah, probably, although from what I understand it also does a copy and conversion to the format you specify, so maybe it's not worth it.,
2/4