Uploaded image for project: 'Qt'
  1. Qt
  2. QTBUG-39359

QtWin::imageFromHBITMAP incorrectly supposes that the format of the HBitmap is always Format_ARGB32_Premultiplied

    XMLWordPrintable

Details

    • Bug
    • Resolution: Duplicate
    • Not Evaluated
    • None
    • 5.3.0
    • Extras: Win
    • None
    • I'm working with the Qt 5.3 on Windows 7 with basic theme, MSVC 2010 32 bits as compiler.

    Description

      I've used the function available on WinExtras to convert from an HBitmap to a QImage, however the image that is returned is different from the original HBitmap.

      Investigating a little bit I found the reason of this issue: The format of the QImage used internally by Qt, the value used is: Format_ARGB32_Premultiplied, but to correctly convert my image I implemented this function but using the QImage::Format_RGB32.

      I belive the solution is give a option to set the format of the output QImage.

      I've atttached a example that reproduces the error, and a possible solution.

      I've attached two images that show the difference between the Qt function to convert HBitmap to QImage and the function that I proposed. I can reproduce these problem more emphatically on Windows 7 with basic theme (not aero), as you can see those images are from Windows 7 with basic theme.

      Final solution that I propose:

       
      QImage imageFromHBITMAP_Improved(HDC hdc, HBITMAP bitmap, QImage::Format format)
      {
          BITMAPINFO bmi = {0};
          bmi.bmiHeader.biSize = sizeof(bmi.bmiHeader);
      
          // Get the BITMAPINFO structure from the bitmap
          if(!GetDIBits(hdc, bitmap, 0, 0, NULL, &bmi, DIB_RGB_COLORS)){
              qErrnoWarning("%s: failed to get bitmap info", __FUNCTION__);
              return QImage();
          }
      
          // Get bitmap bits
          QScopedArrayPointer<uchar> data(new uchar [bmi.bmiHeader.biSizeImage]);
      
          bmi.bmiHeader.biBitCount = 32;
          bmi.bmiHeader.biCompression = BI_RGB;
      
          if(!GetDIBits(hdc, bitmap, 0, bmi.bmiHeader.biHeight, data.data(), &bmi, DIB_RGB_COLORS)){
              qErrnoWarning("%s: failed to get bitmap bits", __FUNCTION__);
              return QImage();
          }
      
          int w = bmi.bmiHeader.biWidth;
          int h = bmi.bmiHeader.biHeight;
      
          return QImage(data.data(), w, h, format).mirrored();
      }
      

      Attachments

        1. imageFromHBITMAP_Improved.png
          121 kB
          Antônio Ferreira Dias Júnior
        2. qtbug39359.zip
          4 kB
          Friedemann Kleint
        3. QtWin-imageFromHBITMAP.png
          115 kB
          Antônio Ferreira Dias Júnior
        4. SampleAndPossibleSolution.zip
          4 kB
          Antônio Ferreira Dias Júnior

        Issue Links

          Activity

            People

              kleint Friedemann Kleint
              antonyprojr Antônio Ferreira Dias Júnior
              Votes:
              0 Vote for this issue
              Watchers:
              3 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved: