Qpixmap Pixmap; The graphics class in the QT Library can handle all the graphics types supported by QT, such as jpg,bmp qpixmap pix; PIX = Pixmap.grabwindow (This->winid (), 0,0,-1,-1); This->winid () for obtaining the current window id//pix = pixmap.grabwidget (this,0,0,-1,-1); Can also use this method, they two difference is only the first parameter is not just the IF (Pix.isnull ()) {qmessagebox::information (this, "error", "Grab-screen failed", Qmessagebox::ok); } else {if (Pix.save ("grab.jpg") ==false) {qmessagebox::information (this, "right", "Save Error", Qmessagebox::ok);} else Qmessagebox::information (This, "Grab", "Bitmap saved as Grab.bmp", Qmessagebox::ok); }
The main methods of implementation are:
① use Qpixmap's Grabwindow to pick the window to grab the picture;
② then uses the bool Qpixmap::save (const QString & fileName, const char * format = 0, int quality =-1) const, named to save the crawled picture.
Note the point:
①:grabwidget represents the canvas portion of the captured window, and Grabwindow grabs the part with the caption box.
②grabwindow If the first parameter is 0, then the entire desktop is crawled.
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
※QT5 is used in the QScreen class, and then uses its Qpixmap qscreen::grabwindow (WId window, int x = 0, int y = 0, Intwidth =-1, int height =-1) method.
QScreen *screen = qguiapplication::p rimaryscreen (); Screen->grabwindow (This->winid (), 0,0,-1,-1). Save ("captureimg.jpg", "jpg");