Take screenshot of MapGraphicsView (in C++)

3249
4
Jump to solution
03-28-2016 01:14 PM
MaximilianSchönenberg
New Contributor II

Hello,

I want to take a screenshot of the MapGraphicsView and export it to png, jpg..

I tried this, but it returns just a grey picture:

//EsriRuntimeQt::MapGraphicsView* m_MapGraphicsView;
auto pixmap = QPixmap::grabWidget(m_MapGraphicsView);
QFile file(fileUrl.toLocalFile());
file.open(QIODevice::WriteOnly);
pixmap.save(&file, "PNG");

Is there another way of taking a screenshot?

Thanks in advance!

Best regards,

Maximilian

0 Kudos
1 Solution

Accepted Solutions
MaximilianSchönenberg
New Contributor II

Thank you for your answer Koushik, but it's the same result as before (see attachment).

This is what finally works for me (thanks to Norbert Thoden​):

//QVBoxLayout* m_VBox
QRect rect(m_VBox->itemAt(1)->geometry());
QMutex m(QMutex::NonRecursive);
m.lock();
m.tryLock(100);
auto pixmap = QGuiApplication::primaryScreen()->grabWindow(winId(),rect.x(), rect.y(), rect.width(), rect.height());
QFile file(fileUrl.toLocalFile());
file.open(QIODevice::WriteOnly);
pixmap.save(&file, "PNG");

Best regards,

Maximilian Schönenberg

View solution in original post

0 Kudos
4 Replies
LucasDanzinger
Esri Frequent Contributor

This doesn't directly help you right now, but I just wanted to let you know that with our Quartz release, there will be a function directly on the MapView that will export it to an image.

MaximilianSchönenberg
New Contributor II

Ok, but Quartz isn't released yet. Is there another way of taking a screenshot of the MapView with 10.2.6?

0 Kudos
KoushikHajra
Esri Contributor

You can try this.  You might want to change the size of the rectangle.  But this should work:

    QRect rect(QPoint(0, 100), QSize(400, 400)) ;
    QPixmap image = QWidget::grab(rect);
    QString path = QDir::tempPath() + QString("/testImage.png");
    bool success = image.save(path);
    qDebug() << success;

Possibly didn't work because there was no size specified maybe?

0 Kudos
MaximilianSchönenberg
New Contributor II

Thank you for your answer Koushik, but it's the same result as before (see attachment).

This is what finally works for me (thanks to Norbert Thoden​):

//QVBoxLayout* m_VBox
QRect rect(m_VBox->itemAt(1)->geometry());
QMutex m(QMutex::NonRecursive);
m.lock();
m.tryLock(100);
auto pixmap = QGuiApplication::primaryScreen()->grabWindow(winId(),rect.x(), rect.y(), rect.width(), rect.height());
QFile file(fileUrl.toLocalFile());
file.open(QIODevice::WriteOnly);
pixmap.save(&file, "PNG");

Best regards,

Maximilian Schönenberg

0 Kudos