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 take a screenshot with grabwindow in RGB format?
Qt 6.11 is out! See what's new in the release blog

How to take a screenshot with grabwindow in RGB format?

Scheduled Pinned Locked Moved Unsolved General and Desktop
15 Posts 4 Posters 2.3k 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.
  • Christian EhrlicherC Offline
    Christian EhrlicherC Offline
    Christian Ehrlicher
    Lifetime Qt Champion
    wrote on last edited by
    #4

    When you retrieved the QImage you can transform it as you like - see the documentation: https://doc.qt.io/qt-6/qimage.html

    Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
    Visit the Qt Academy at https://academy.qt.io/catalog

    1 Reply Last reply
    1
    • R Offline
      R Offline
      rohan136
      wrote on last edited by rohan136
      #5

      I want to store the screenshot (whose size should be 50 x 50 in RGB format) in an array. Below is my implementation.

      QByteArray ImageData;
          QByteArray bytes;
          QImage RGB_Image;
      
          qInfo() << "Sending Data";
      
          QScreen *screen = QGuiApplication::primaryScreen();
          QPixmap ScreenImage;
      
          ScreenImage = screen->grabWindow(0,0,0,50,50);
          ScreenImage.save("Screenshot.png");
      
      
          RGB_Image = ScreenImage.toImage();
          RGB_Image.convertTo(QImage::Format_RGB32);
      
          qInfo() << "RGB size " << RGB_Image.size();
      
          ImageData.clear();
      
          QDataStream out(&ImageData, QIODevice :: WriteOnly);
          out << RGB_Image;
      
          qInfo()<<"RGB Array size "<<ImageData.size();
      

      The size of an array should be 7500(50 x 50 x 3) but I'm getting the RGB Array size 1101 and RGB size QSize(63, 63).

      What am i doing wrong?

      JonBJ 1 Reply Last reply
      0
      • R rohan136

        I want to store the screenshot (whose size should be 50 x 50 in RGB format) in an array. Below is my implementation.

        QByteArray ImageData;
            QByteArray bytes;
            QImage RGB_Image;
        
            qInfo() << "Sending Data";
        
            QScreen *screen = QGuiApplication::primaryScreen();
            QPixmap ScreenImage;
        
            ScreenImage = screen->grabWindow(0,0,0,50,50);
            ScreenImage.save("Screenshot.png");
        
        
            RGB_Image = ScreenImage.toImage();
            RGB_Image.convertTo(QImage::Format_RGB32);
        
            qInfo() << "RGB size " << RGB_Image.size();
        
            ImageData.clear();
        
            QDataStream out(&ImageData, QIODevice :: WriteOnly);
            out << RGB_Image;
        
            qInfo()<<"RGB Array size "<<ImageData.size();
        

        The size of an array should be 7500(50 x 50 x 3) but I'm getting the RGB Array size 1101 and RGB size QSize(63, 63).

        What am i doing wrong?

        JonBJ Offline
        JonBJ Offline
        JonB
        wrote on last edited by JonB
        #6

        @rohan136
        I don't know, but let's start here:

        The size of an array should be 7500(50 x 50 x 3)

        No, it should be x 4. I make that 10,000.

        I'm getting the RGB Array size 1101

        No chance you might mean something like 10101, which might be 10,000 + some image information overhead?

        RGB size QSize(63, 63).

        Any chance https://doc.qt.io/qt-6/qscreen.html#grabWindow

        The offset and size arguments are specified in device independent pixels. The returned pixmap may be larger than the requested size when grabbing from a high-DPI screen. Call QPixmap::devicePixelRatio() to determine if this is the case.

        Whatever you think of the figures, if you deserialize the image data and show it does it come out being correct for the original area grabbed?

        1 Reply Last reply
        0
        • R Offline
          R Offline
          rohan136
          wrote on last edited by rohan136
          #7

          @JonB ,

          I set the DevicePixelRatio to 1, it was 1.25 when i checked with devicePixelRatio(). However, I'm getting the following output
          RGB size QSize(63, 63)
          RGB Array size 829 (Its not like 10101 as per the implementation I shared, am i making any mistake for storing image in array?)

          Image is displayed correctly. But my concern is the size of the data because I need to transfer the Image data via Wifi to the device where the size of the rgb image should be fixed i.e. 7500 in rgb format. Can you tell me how to achieve this?

          1 Reply Last reply
          0
          • JonBJ Offline
            JonBJ Offline
            JonB
            wrote on last edited by JonB
            #8

            For the RGB Array size. Although I would expect ImageData.size() to return the correct, whole size maybe it's possible that the QDataStream has not flushed the data to the ImageData variable? Does:

            {
                QDataStream out(&ImageData, QIODevice :: WriteOnly);
                out << RGB_Image;
            }
            

            (note the { ... }) make any difference?

            to the device where the size of the rgb image should be fixed i.e. 7500 in rgb format

            I don't know what format that would be. As I said QImage::Format_RGB32 is, as the name tells you, 32-bits per pixel not 24. Maybe https://doc.qt.io/qt-6/qimage.html#Format-enum

            QImage::Format_RGB888 13 The image is stored using a 24-bit RGB format (8-8-8).

            suits you, or you would need to do some manipulation on the QImage::Format_RGB32-generated format.

            P.S.
            Although I think QImage::convertTo() should work in-place, the docs actually ask you to use QImage::convertToFormat() (returns a new copy instead). Any difference?

            1 Reply Last reply
            0
            • R Offline
              R Offline
              rohan136
              wrote on last edited by
              #9

              @JonB ,

              No difference by {..} and I tried with convertToFormat() but no difference. I'm getting the same size.

              JonBJ 1 Reply Last reply
              0
              • R rohan136

                @JonB ,

                No difference by {..} and I tried with convertToFormat() but no difference. I'm getting the same size.

                JonBJ Offline
                JonBJ Offline
                JonB
                wrote on last edited by JonB
                #10

                @rohan136
                Can you save the converted-to-ImageFormat to a file as a .png or whatever is right for the format you use (not my area), see if it loads as the correct image and check its file size? At that point I think this is beyond my knowledge.

                1 Reply Last reply
                0
                • R Offline
                  R Offline
                  rohan136
                  wrote on last edited by rohan136
                  #11

                  @JonB
                  It is storing the almost same size (954) as displayed by the array size(958), and it loads the correct image

                  1 Reply Last reply
                  0
                  • Christian EhrlicherC Offline
                    Christian EhrlicherC Offline
                    Christian Ehrlicher
                    Lifetime Qt Champion
                    wrote on last edited by
                    #12

                    When you look into the ImageData you will notice, that the data is not stored uncompressed but as png.
                    If you want to access the bits use QImage::bits()

                    Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                    Visit the Qt Academy at https://academy.qt.io/catalog

                    JonBJ 1 Reply Last reply
                    1
                    • Christian EhrlicherC Christian Ehrlicher

                      When you look into the ImageData you will notice, that the data is not stored uncompressed but as png.
                      If you want to access the bits use QImage::bits()

                      JonBJ Offline
                      JonBJ Offline
                      JonB
                      wrote on last edited by
                      #13

                      @Christian-Ehrlicher Ah, so this pic has been compressed up to 90% as png!

                      1 Reply Last reply
                      0
                      • R Offline
                        R Offline
                        rohan136
                        wrote on last edited by
                        #14

                        How to store the image bytes in an array by using QImage::bits()?

                        jsulmJ 1 Reply Last reply
                        0
                        • R rohan136

                          How to store the image bytes in an array by using QImage::bits()?

                          jsulmJ Online
                          jsulmJ Online
                          jsulm
                          Lifetime Qt Champion
                          wrote on last edited by
                          #15

                          @rohan136 said in How to take a screenshot with grabwindow in RGB format?:

                          How to store the image bytes in an array by using QImage::bits()?

                          bits() returns a char* which can be treated like an array (just like any other pointer in C/C++).

                          https://forum.qt.io/topic/113070/qt-code-of-conduct

                          1 Reply Last reply
                          1

                          • Login

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