To capture current screenshot and save it to the local machine, we can use the following code –
package com.work_02; import java.awt.Rectangle; import java.awt.Robot; import java.awt.Toolkit; import java.awt.image.BufferedImage; import java.io.File; import javax.imageio.ImageIO; public class MyScreen { public static void main(String[] args) { try{ Robot robot = new Robot(); BufferedImage screenShot = robot.createScreenCapture(new Rectangle(Toolkit.getDefaultToolkit().getScreenSize())); ImageIO.write(screenShot, "JPG", new File("D:\\MyScreenShot.jpg")); System.out.println("Done"); } catch(Exception ex) { ex.printStackTrace(); } } }