Take Screenshot of Each Monitor Individually 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();
        GraphicsDevice[] gd = ge.getScreenDevices();

        for (int i = 0; i < gd.length; i++) {
            Rectangle rectangle = gd[i].getDefaultConfiguration().getBounds();
            BufferedImage image = new Robot().createScreenCapture(rectangle);
            ImageIO.write(image, "png", new File("screenshot_" + i + ".png"));
        }
    }
}

Leave a Comment

Cancel reply

Your email address will not be published.