void ScreenSelector::grabColor() { m_selectionRect = m_selectionRect.normalized(); QDesktopWidget* desktop = QApplication::desktop(); int screenNum = desktop->screenNumber(m_selectionRect.topLeft()); QScreen* screen = QGuiApplication::screens()[screenNum]; QPixmap screenGrab = screen->grabWindow(desktop->winId(), m_selectionRect.x(), m_selectionRect.y(), m_selectionRect.width(), m_selectionRect.height()); QImage image = screenGrab.toImage(); int numPixel = image.width() * image.height(); int sumR = 0; int sumG = 0; int sumB = 0; for (int x = 0; x < image.width(); ++x) { for (int y = 0; y < image.height(); ++y) { QColor color = image.pixel(x, y); sumR += color.red(); sumG += color.green(); sumB += color.blue(); } } QColor avgColor(sumR / numPixel, sumG / numPixel, sumB / numPixel); emit colorPicked(avgColor); }
void Screenshot::dumpFullScreen() { QDesktopWidget *desktop = QApplication::desktop(); originalPixmap = QPixmap(); originalPixmap = QPixmap::grabWindow(desktop->winId(), 0, 0, desktop->width(), desktop->height()); currentPixmap = originalPixmap; }
QPixmap ScreenShooter::captureRect(const QRect &rect) { #if (QT_VERSION >= QT_VERSION_CHECK(5, 0, 0)) return QGuiApplication::primaryScreen()->grabWindow(0, rect.x(), rect.y(), rect.width(), rect.height()); #else QDesktopWidget *desktop = QApplication::desktop(); return QPixmap::grabWindow(desktop->winId(), rect.x(), rect.y(), rect.width(), rect.height()); #endif }
QPixmap UBDesktopAnnotationController::getScreenPixmap() { QDesktopWidget *desktop = QApplication::desktop(); // we capture the screen in which the mouse is. const QRect primaryScreenRect = desktop->screenGeometry(QCursor::pos()); QCoreApplication::flush(); return QPixmap::grabWindow(desktop->winId(), primaryScreenRect.x() , primaryScreenRect.y(), primaryScreenRect.width(), primaryScreenRect.height()); }
QPixmap CScreenShotView::createDesktopPixmap() { #ifdef Q_OS_MAC QPixmap pixmap = getScreenPixmap(m_desktopScreen); #else QDesktopWidget *pDesktoWidget = QApplication::desktop(); QPixmap pixmap = m_desktopScreen->grabWindow(pDesktoWidget->winId(),pDesktoWidget->geometry().x() ,pDesktoWidget->geometry().y(),pDesktoWidget->geometry().width(),pDesktoWidget->geometry().height()); #endif return pixmap; }
//---------------------------------------------------------------------------- void GLWidget::captureScreen() { //LOG_ENTER_FUNC(); QDesktopWidget *desktop = QApplication::desktop(); _screenshot = QPixmap::grabWindow( desktop->winId(), 0, 0, desktop->width(), desktop->height() ); }
QList<QPair<QPixmap, QRect> > ScreenShooter::captureWindows(const QList<WindowHandle> &windows) { QDesktopWidget *desktop = QApplication::desktop(); QList< QPair<QPixmap, QRect> > result; for(const WindowHandle &window: windows) { if(!window.isValid()) continue; const QRect &windowGeometry = window.rect(); #if (QT_VERSION >= QT_VERSION_CHECK(5, 0, 0)) result.append(qMakePair(QGuiApplication::primaryScreen()->grabWindow(desktop->winId(), windowGeometry.x(), windowGeometry.y(), windowGeometry.width(), windowGeometry.height()), windowGeometry)); #else result.append(qMakePair(QPixmap::grabWindow(desktop->winId(), windowGeometry.x(), windowGeometry.y(), windowGeometry.width(), windowGeometry.height()), windowGeometry)); #endif } return result; }
int main(int argc, char** argv) { QApplication app(argc, argv); QDesktopWidget *desktop = QApplication::desktop(); float scale = dpiScaleFactor(desktop); qDebug() << "Scale factor" << scale; QPixmap screenshot = QPixmap::grabWindow(desktop->winId(), 0, 0, desktop->width()*scale, desktop->height()*scale); QFile file("screenshot.png"); file.open(QIODevice::WriteOnly); screenshot.save(&file, "JPEG", 100);//(-1..0-100) 0 smallest lowest quality return app.exec(); }
QPixmap ScreenShooter::captureWindow(WindowHandle window) { if(!window.isValid()) return QPixmap(); const QRect &windowGeometry = window.rect(); #if (QT_VERSION >= QT_VERSION_CHECK(5, 0, 0)) return QGuiApplication::primaryScreen()->grabWindow(0, windowGeometry.x(), windowGeometry.y(), windowGeometry.width(), windowGeometry.height()); #else QDesktopWidget *desktop = QApplication::desktop(); return QPixmap::grabWindow(desktop->winId(), windowGeometry.x(), windowGeometry.y(), windowGeometry.width(), windowGeometry.height()); #endif }
QPixmap ScreenShooter::captureScreen(int screenIndex) { QDesktopWidget *desktop = QApplication::desktop(); if(screenIndex < 0 || screenIndex >= desktop->screenCount()) return QPixmap(); const QRect &screenGeometry = desktop->screenGeometry(screenIndex); #if (QT_VERSION >= QT_VERSION_CHECK(5, 0, 0)) return QGuiApplication::primaryScreen()->grabWindow(0, screenGeometry.x(), screenGeometry.y(), screenGeometry.width(), screenGeometry.height()); #else return QPixmap::grabWindow(desktop->winId(), screenGeometry.x(), screenGeometry.y(), screenGeometry.width(), screenGeometry.height()); #endif }
void MainWindow::on_screenShot() { QPixmap pixmap; QDesktopWidget *desktop = QApplication::desktop(); pixmap = QPixmap::grabWindow(desktop->winId(), pos().x(), pos().y(), frameGeometry().width(), frameGeometry().height()); QString format = "png"; QString initialPath = QDir::currentPath()+ tr("/untitled.") + format; QString fileName = QFileDialog::getSaveFileName(this, tr("Save As"),initialPath, tr("%1 Files (*.%2);;All Files (*)") .arg(format.toUpper()).arg(format)); if (!fileName.isEmpty()) pixmap.save(fileName, format.toLatin1()); }
QList< QPair<QPixmap, QRect> > ScreenShooter::captureScreens() { QDesktopWidget *desktop = QApplication::desktop(); QList< QPair<QPixmap, QRect> > result; for(int screenIndex = 0; screenIndex < desktop->screenCount(); ++screenIndex) { const QRect &screenGeometry = desktop->screenGeometry(screenIndex); #if (QT_VERSION >= QT_VERSION_CHECK(5, 0, 0)) result.append(qMakePair(QGuiApplication::primaryScreen()->grabWindow(0, screenGeometry.x(), screenGeometry.y(), screenGeometry.width(), screenGeometry.height()), screenGeometry)); #else result.append(qMakePair(QPixmap::grabWindow(desktop->winId(), screenGeometry.x(), screenGeometry.y(), screenGeometry.width(), screenGeometry.height()), screenGeometry)); #endif } return result; }
void AppView::makeScreenshot() { // Making screenshot QDesktopWidget* desktop = QApplication::desktop(); QRect geo = desktop->screenGeometry(desktop->screenNumber(QCursor::pos())); screenshot_ = QGuiApplication::primaryScreen()->grabWindow(desktop->winId(), geo.left(), geo.top(), geo.width(), geo.height()); int width = screenshot_.width(), height = screenshot_.height(); setGeometry(0, 0, width, height); scene_.clear(); scene_.setSceneRect(0, 0, width, height); reinitVisibleArea(); // Background screenshot scene_.addPixmap(screenshot_); showFullScreen(); }
KSnapshot::KSnapshot(QWidget *parent, KSnapshotObject::CaptureMode mode ) : QDialog(parent), KSnapshotObject(), modified(true), savedPosition(QPoint(-1, -1)) { // TEMPORARY Make sure "untitled" enters the string freeze for 4.6, // as explained in http://lists.kde.org/?l=kde-graphics-devel&m=128942871430175&w=2 const QString untitled = QString(tr("untitled")); setWindowTitle(""); grabber = new QWidget( 0, Qt::X11BypassWindowManagerHint ); // TODO X11 (Xinerama and Twinview, actually) and Windows use different coordinates for the two monitors case // // On Windows, there are two displays. The origin (0, 0) ('o') is the top left of display 1. If display 2 is to the left, then coordinates in display 2 are negative: // .-------. // | |o-----. // | 2 | | // | | 1 | // ._______.._____. // // On Xinerama and Twinview, there is only one display and two screens. The origin (0, 0) ('o') is the top left of the display: // o-------. // | |.-----. // | 2 | | // | | 1 | // ._______.._____. // // Instead of moving to (-10000, -10000), we should compute how many displays are and make sure we move to somewhere out of the total coordinates. // - Windows: use GetSystemMetrics ( http://msdn.microsoft.com/en-us/library/ms724385(v=vs.85).aspx ) // If moving to a negative position, we need to count the size of the dialog; moving to a positive position avoids having to compute the size of the dialog grabber->move( -10000, -10000 ); // FIXME Read above grabber->installEventFilter( this ); QVBoxLayout *vbox = new QVBoxLayout( this ); setLayout(vbox); mainWidget = new KSnapshotWidget(); vbox->addWidget(mainWidget); connect(mainWidget->ok_btn, SIGNAL(clicked()), SLOT(onOkBtnClicked())); connect(mainWidget->cancel_btn, SIGNAL(clicked()), SLOT(onCancelBtnClicked())); connect(mainWidget->save_btn, SIGNAL(clicked()), SLOT(onSaveBtnClicked())); connect(mainWidget->help_btn, SIGNAL(clicked()), SLOT(onHelpBtnClicked())); connect(mainWidget->lblImage, SIGNAL(startDrag()), SLOT(slotDragSnapshot())); connect(mainWidget->btnNew, SIGNAL(clicked()), SLOT(slotGrab())); connect(mainWidget->comboMode, SIGNAL(activated(int)), SLOT(slotModeChanged(int))); if (qApp->desktop()->numScreens() < 2) { mainWidget->comboMode->removeItem(CurrentScreen); } mainWidget->spinDelay->setSuffix(tr(" second", " seconds")); grabber->show(); grabber->grabMouse(); #ifdef HAVE_X11_EXTENSIONS_XFIXES_H { int tmp1, tmp2; //Check whether the XFixes extension is available Display *dpy = QX11Info::display(); if (!XFixesQueryExtension( dpy, &tmp1, &tmp2 )) { mainWidget->cbIncludePointer->hide(); mainWidget->lblIncludePointer->hide(); } } #elif !defined(Q_WS_WIN) mainWidget->cbIncludePointer->hide(); mainWidget->lblIncludePointer->hide(); #endif setMode(KSnapshotObject::Region); qDebug() << "Mode = " << mode; if ( mode == KSnapshotObject::FullScreen ) { snapshot = QPixmap::grabWindow( QApplication::desktop()->winId() ); #ifdef HAVE_X11_EXTENSIONS_XFIXES_H if ( haveXFixes && includePointer() ) grabPointerImage(0, 0); #endif } else if ( mode == KSnapshotObject::CurrentScreen ) { qDebug() << "Desktop Geom = " << QApplication::desktop()->geometry(); QDesktopWidget *desktop = QApplication::desktop(); int screenId = desktop->screenNumber( QCursor::pos() ); qDebug() << "Screenid = " << screenId; QRect geom = desktop->screenGeometry( screenId ); qDebug() << "Geometry = " << screenId; snapshot = QPixmap::grabWindow( desktop->winId(), geom.x(), geom.y(), geom.width(), geom.height() ); } else { setMode( mode ); switch(mode) { case KSnapshotObject::WindowUnderCursor: { setIncludeDecorations( true ); performGrab(); break; } case KSnapshotObject::ChildWindow: { slotGrab(); break; } case KSnapshotObject::Region: { grabRegion(); break; } case KSnapshotObject::FreeRegion: { grabFreeRegion(); break; } default: break; } } //When we use argument to take snapshot we mustn't hide it. if (mode != KSnapshotObject::ChildWindow) { grabber->releaseMouse(); grabber->hide(); } setDelay(0); file_path_ = QQGlobal::tempDir() + "/snapshot/snapshot.png"; connect( &grabTimer, SIGNAL(timeout()), this, SLOT(grabTimerDone()) ); connect( &updateTimer, SIGNAL(timeout()), this, SLOT(updatePreview()) ); QTimer::singleShot( 0, this, SLOT(updateCaption()) ); new QShortcut( Qt::Key_S, mainWidget->ok_btn, SLOT(animateClick())); new QShortcut( Qt::Key_N, mainWidget->btnNew, SLOT(animateClick()) ); new QShortcut( Qt::Key_Space, mainWidget->btnNew, SLOT(animateClick()) ); mainWidget->ok_btn->setFocus(); resize(QSize(400, 500)); move((QApplication::desktop()->width() - this->width()) /2, (QApplication::desktop()->height() - this->height()) /2); }
void KSnapshot::performGrab() { int x = 0; int y = 0; grabber->releaseMouse(); grabber->hide(); grabTimer.stop(); title.clear(); windowClass.clear(); if ( mode() == ChildWindow ) { WindowGrabber wndGrab; connect( &wndGrab, SIGNAL(windowGrabbed(QPixmap)), SLOT(slotWindowGrabbed(QPixmap)) ); wndGrab.exec(); QPoint offset = wndGrab.lastWindowPosition(); x = offset.x(); y = offset.y(); qDebug() << "last window position is" << offset; } else if ( mode() == WindowUnderCursor ) { snapshot = WindowGrabber::grabCurrent( includeDecorations() ); QPoint offset = WindowGrabber::lastWindowPosition(); x = offset.x(); y = offset.y(); // If we're showing decorations anyway then we'll add the title and window // class to the output image meta data. if ( includeDecorations() ) { title = WindowGrabber::lastWindowTitle(); } } else if ( mode() == CurrentScreen ) { qDebug() << "Desktop Geom2 = " << QApplication::desktop()->geometry(); QDesktopWidget *desktop = QApplication::desktop(); int screenId = desktop->screenNumber( QCursor::pos() ); qDebug() << "Screenid2 = " << screenId; QRect geom = desktop->screenGeometry( screenId ); qDebug() << "Geometry2 = " << geom; x = geom.x(); y = geom.y(); snapshot = QPixmap::grabWindow( desktop->winId(), x, y, geom.width(), geom.height() ); } else { snapshot = QPixmap::grabWindow( QApplication::desktop()->winId() ); } #ifdef HAVE_X11_EXTENSIONS_XFIXES_H if (haveXFixes && includePointer()) { grabPointerImage(x, y); } #endif // HAVE_X11_EXTENSIONS_XFIXES_H if ( snapshot.isNull() ) { mainWidget->ok_btn->setEnabled(false); mainWidget->save_btn->setEnabled(false); } else { mainWidget->ok_btn->setEnabled(true); mainWidget->save_btn->setEnabled(true); } updatePreview(); QApplication::restoreOverrideCursor(); modified = true; updateCaption(); if (savedPosition != QPoint(-1, -1)) { move(savedPosition); } show(); }
QColor Overlay::grabScreenColor(const QPoint &p) { QDesktopWidget *desktop = QApplication::desktop(); QPixmap pixmap = QGuiApplication::screens().at(desktop->screenNumber())->grabWindow(desktop->winId(), p.x(), p.y(), 1, 1); QImage i = pixmap.toImage(); return i.pixel(0, 0); }