How to take a screenshot with grabwindow in RGB format?
-
Simply use QScreen::grabWindow().
-
When you retrieved the QImage you can transform it as you like - see the documentation: https://doc.qt.io/qt-6/qimage.html
-
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?
-
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?
@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?
-
@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?
-
For the RGB Array size. Although I would expect
ImageData.size()to return the correct, whole size maybe it's possible that theQDataStreamhas not flushed the data to theImageDatavariable? 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_RGB32is, as the name tells you, 32-bits per pixel not 24. Maybe https://doc.qt.io/qt-6/qimage.html#Format-enumQImage::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? -
@JonB ,
No difference by {..} and I tried with convertToFormat() but no difference. I'm getting the same size.
-
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() -
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()@Christian-Ehrlicher Ah, so this pic has been compressed up to 90% as
png! -
@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++).