This is a short class to take screenshots on mutltiple monitor systems, I made some modifications to the solution at http://stackoverflow.com/questions/10042086/screen-capture-in-java-not-capturing-whole-screen so that the code coped with monitors at different hights and so on. Any and all suggestions welcome :)
/**
* Code modified from code given in http://whileonefork.blogspot.co.uk/2011/02/java-multi-monitor-screenshots.html following a SE question at
* http://stackoverflow.com/questions/10042086/screen-capture-in-java-not-capturing-whole-screen
*/
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.image.BufferedImage;
import java.io.File;
import javax.imageio.ImageIO;
class ScreenCapture {
static int numberOfMinutesToSleepBetweenScreenshots = 4;
public static void main(String args[]) throws Exception {
int i = 1000;
while (true) {
takeScreenshot("ScreenCapture" + i++);
try {
Thread.sleep(60 * numberOfMinutesToSleepBetweenScreenshots * 1000);
} catch (Exception e) {
e.printStackTrace();
}
}
}
public static void takeScreenshot(String filename) throws Exception {
// Okay so all we have to do here is find the screen with the lowest x,
// the screen with the lowest y, the screen with the higtest value of X+
// width and the screen with the highest value of Y+height
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
GraphicsDevice[] screens = ge.getScreenDevices();
Rectangle allScreenBounds = new Rectangle();
int farx = 0;
int fary = 0;
for (GraphicsDevice screen : screens) {
Rectangle screenBounds = screen.getDefaultConfiguration().getBounds();
//finding the one corner
if (allScreenBounds.x > screenBounds.x) {
allScreenBounds.x = screenBounds.x;
}
if (allScreenBounds.y > screenBounds.y) {
allScreenBounds.y = screenBounds.y;
}
//finding the other corner
if (farx < (screenBounds.x + screenBounds.width)) {
farx = screenBounds.x + screenBounds.width;
}
if (fary < (screenBounds.y + screenBounds.height)) {
fary = screenBounds.y + screenBounds.height;
}
allScreenBounds.width = farx - allScreenBounds.x;
allScreenBounds.height = fary - allScreenBounds.y;
}
Robot robot = new Robot();
BufferedImage screenShot = robot.createScreenCapture(allScreenBounds);
ImageIO.write(screenShot, "jpg", new File(filename + ".jpg"));
}
}
EDIT - thank you everybody, upvotes for all!