Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to convert GDI bitmap to QPixmap

How to convert GDI bitmap to QPixmap

Scheduled Pinned Locked Moved Unsolved General and Desktop
4 Posts 2 Posters 614 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    Cesar
    wrote on 11 Sept 2022, 00:46 last edited by
    #1

    How to convert a GDI GpBitmap* to QPixmap?

    1 Reply Last reply
    0
    • C Offline
      C Offline
      Chris Kawa
      Lifetime Qt Champion
      wrote on 11 Sept 2022, 02:11 last edited by
      #2

      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.

      1 Reply Last reply
      1
      • C Offline
        C Offline
        Cesar
        wrote on 11 Sept 2022, 03:00 last edited by Cesar 9 Nov 2022, 03:02
        #3

        Does it preserve transparency?

        get access to the GpBitmap raw data and create QImage from that directly

        Do you mean lockbits?

        C 1 Reply Last reply 11 Sept 2022, 14:13
        0
        • C Cesar
          11 Sept 2022, 03:00

          Does it preserve transparency?

          get access to the GpBitmap raw data and create QImage from that directly

          Do you mean lockbits?

          C Offline
          C Offline
          Chris Kawa
          Lifetime Qt Champion
          wrote on 11 Sept 2022, 14:13 last edited by
          #4

          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.,

          1 Reply Last reply
          0

          2/4

          11 Sept 2022, 02:11

          • Login

          • Login or register to search.
          2 out of 4
          • First post
            2/4
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved