21
$\begingroup$

Is it possible to get a screenshot of the entire desktop directly in Mathematica?

I am on Ubuntu running Mathematica 8. For some reason GUIScreenShot[] fails. But something like GUIScreenShot[{{0, 500}, {0, 500}}] works

It is strange that in the reference

GUIScreenShot[]

is given as an example (last one), with the caption Capture the entire screen:. But when I copy it and paste it in a notebook, it expects a parameter! (I have included the code Needs["GUIKit`"])

I get the error as shown in this image

Screenshot of the image

$\endgroup$
6
  • 2
    $\begingroup$ I am trying to automate a simple game (!). So grabbing it using an external program, then saving it as a file and importing the file again from within Mathematica is a tedious job (if not realtime). Also I am not sure if I can pipe the output of a program as input to Mathematica (I could not find a way to get it done with Run[] command! - i.e. the piping large output from external program) $\endgroup$ Aug 11, 2012 at 23:16
  • $\begingroup$ GUIScreenShot[] works here (Win XP) $\endgroup$ Aug 11, 2012 at 23:34
  • $\begingroup$ GUIScreenShot[] doesn't work for me (Arch Linux), it spits this error message on the terminal. Looks like a Linux issue... $\endgroup$
    – Renan
    Aug 12, 2012 at 0:14
  • $\begingroup$ It works fine on my win7_x64 and mma8.0.4 (although a syntax indicator). Didn't know this function before. Very helpful! Thanks and +1! $\endgroup$
    – Silvia
    Aug 12, 2012 at 6:52
  • $\begingroup$ GUIScreenShot[] works in version 7 under Windows 7. $\endgroup$
    – Mr.Wizard
    Aug 12, 2012 at 7:55

5 Answers 5

16
$\begingroup$

GUIScreenShot[] (without arguments) works perfectly fine on my Mac, although the front end syntax highlighter indicates a missing argument. As a workaround, you can explicitly give it the dimensions of your screen so that all of it is captured. Here's how you can programmatically get your screen size and use it:

Needs["GUIKit`"]
GUIScreenShot["FullScreenArea" /. Flatten@SystemInformation["Devices", "ScreenInformation"]]
$\endgroup$
6
  • 1
    $\begingroup$ This does not seem to work on Ubuntu .. Can any1 confirm this ... $\endgroup$ Aug 12, 2012 at 0:58
  • $\begingroup$ @myaccount_ram I'm having the same problem, but with Arch Linux. $\endgroup$
    – Renan
    Aug 12, 2012 at 4:00
  • $\begingroup$ @myaccount_ram It doesn't work on Fedora. $\endgroup$ Aug 12, 2012 at 7:34
  • $\begingroup$ I got ** Raster data contains 0 values per pixel; 3 or 4 are required with ColorFunction->RGBColor** under Linux 64. $\endgroup$ Aug 12, 2012 at 20:39
  • 1
    $\begingroup$ @everyone: The issue seems to be something related to linux and not Mathematica. I would've helped if I could, but I don't have a license to use on my Linux machine. $\endgroup$
    – rm -rf
    Aug 13, 2012 at 1:53
8
$\begingroup$

In v11.2 you can just use CurrentScreenImage[].

$\endgroup$
7
$\begingroup$

A method from Kuba's answer, no GUIKit` is needed. Tested on Mathematica 10.1.

Module[{masknb, img},
       masknb = CreateWindow[{}, WindowSize -> Full, Visible -> False];
       img = MathLink`CallFrontEnd[FrontEnd`NotebookImage[masknb]];
       NotebookClose[masknb];
       img
      ]

We can even make a fairly fast screen recorder from it:

Clear[maskWindow]
maskWindow[width_, height_, leftMarg_, topMarg_] :=
    CreateWindow[{},
        WindowSize -> {width, height},
        WindowMargins -> {{leftMarg, Automatic}, {topMarg, 0}},
        Visible -> False]

Clear[screenSnapshot]
screenSnapshot[window_] := 
    MathLink`CallFrontEnd[FrontEnd`NotebookImage[window]]

screenRecord = {};
recordingFlag = True;
Column[{
        "Click to stop recording:",
        Toggler[Dynamic[recordingFlag],
            {True -> Framed["Recording"], False -> Framed["Stopped  "]}
            ]
        }]

Module[{masknb},
        masknb = maskWindow[500, 300, 300, 200];
        While[recordingFlag,
            screenRecord = Join[screenRecord, {screenSnapshot[masknb]}]
            ];
        NotebookClose[masknb];
        ];
screenRecord = Image /@ screenRecord;
ListAnimate[screenRecord]

MMA screen recorder

$\endgroup$
2
  • $\begingroup$ Extremely Fast!!!!! Very impressive answer~ xie xie la~ but still, are there faster methods? $\endgroup$
    – Wjx
    Aug 25, 2016 at 14:25
  • $\begingroup$ @Wjx Thanks! For better performance, I'd go win API + NETLink. $\endgroup$
    – Silvia
    Aug 26, 2016 at 8:51
5
$\begingroup$

Confirming @R.M's answer (WinXP, Mac OSX 10.8)

enter image description here

$\endgroup$
7
  • 1
    $\begingroup$ why not a comment? Also, why are you hiding your porn tabs? ;) $\endgroup$
    – rm -rf
    Aug 11, 2012 at 23:44
  • $\begingroup$ @rm -rf The porn tabs are there ... look again $\endgroup$ Aug 11, 2012 at 23:45
  • 1
    $\begingroup$ @belisarius Nest[GUIScreenShot[], "", 10] $\endgroup$
    – s0rce
    Aug 12, 2012 at 3:29
  • 1
    $\begingroup$ @belisarius It's not a Droste effect until it is. $\endgroup$
    – rm -rf
    Aug 12, 2012 at 11:57
  • 1
    $\begingroup$ The Droste Effect: Dynamic[Export["C:\\droste.png", GUIScreenShot[]]; Style[GUIScreenShot[], Magnification -> 1], UpdateInterval -> 3]. When the Dynamic object is not visible, it is not updated (it is sufficient just to reduce the Mathematica window). $\endgroup$ Aug 12, 2012 at 18:33
4
$\begingroup$

Using JLink,you can do this

Needs["JLink`"];
InstallJava[];
robotclass = JavaNew["java.awt.Robot"];
LoadJavaClass["javax.imageio.ImageIO"];
LoadJavaClass["java.awt.Toolkit"];
screenSize = Toolkit`getDefaultToolkit[]@getScreenSize[];
image = robotclass@createScreenCapture[JavaNew["java.awt.Rectangle", 0, 0, screenSize@width, screenSize@height]];
ImageIO`write[image, "png",JavaNew["java.io.File", FileNameJoin[{Directory[], "test.png"}]]];
UninstallJava[]

Import["test.png"]

enter image description here

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.