Take Single Screenshot of All Monitors using Java

java.awt package

package app;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

public class Main
{
    public static void main(String[] args) throws AWTException, IOException
    {
        GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();

        Rectangle rectangle = new Rectangle();
        for (GraphicsDevice gd : ge.getScreenDevices()) {
            rectangle = rectangle.union(gd.getDefaultConfiguration().getBounds());
        }

        BufferedImage image = new Robot().createScreenCapture(rectangle);
        ImageIO.write(image, "png", new File("screenshot.png"));
    }
}

Leave a Comment

Cancel reply

Your email address will not be published.