Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

For a screenshot of the computer’s desktop or a selected screen region, use Java’s java.awt.Robot. Its createScreenCapture method returns a BufferedImage, which you can save or process. If your application is built with JavaFX, use javafx.scene.robot.Robot instead. Both approaches capture screen pixels, so they require a graphical desktop and may be affected by operating-system permissions.

Capture the desktop or a screen region with AWT Robot

In a standard Java desktop application, create a Robot and pass it a Rectangle describing the screen area to capture. The rectangle’s x and y values are screen coordinates; width and height must be greater than zero.

import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.image.BufferedImage;
import javax.imageio.ImageIO;
import java.io.File;
import java.io.IOException;
import java.awt.AWTException;

public class DesktopScreenshot {
    public static void main(String[] args) throws AWTException, IOException {
        int x = 0;
        int y = 0;
        int width = 1280;
        int height = 720;

        Robot robot = new Robot();
        BufferedImage image = robot.createScreenCapture(
            new Rectangle(x, y, width, height)
        );

        ImageIO.write(image, "png", new File("screenshot.png"));
    }
}

Change the rectangle to the area you want and the output filename or image format as needed. The sample writes a PNG using Java’s ImageIO; the capture method itself returns image data rather than choosing a file format or destination.

Capture a particular monitor

For a specific display, AWT provides a Robot(GraphicsDevice) constructor. Select the relevant graphics device and use its screen-coordinate arrangement when defining the rectangle. Multi-monitor coordinate layouts can differ by platform, so do not assume every display starts at (0, 0).

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Run capture work off the AWT event thread

Screen capture can take time, particularly when the system needs to request permission. In a graphical AWT or Swing application, do not run the capture operation on the AWT Event Dispatch Thread; perform it in a worker thread so the interface can continue responding.

Use JavaFX Robot in a JavaFX application

If your application already uses JavaFX, javafx.scene.robot.Robot.getScreenCapture(...) captures an area relative to the primary screen and returns a WritableImage. Construct and use this Robot on the JavaFX Application Thread.

JavaFX’s screen-capture overload includes a scaleToFit option. When it is false, HiDPI scaling can make the actual image dimensions differ from the requested dimensions; when true, the result is scaled to the requested size. Choose based on whether you need the requested output dimensions or the unscaled pixel dimensions.

Check headless mode, permissions, and display scaling

  • No graphical desktop: AWT Robot construction throws AWTException when the graphics environment is headless. A server process without a desktop cannot rely on this screen-capture approach.
  • Screen access denied: Desktop permissions or operating-system restrictions may cause a SecurityException; in some denied-permission cases, the API documents that returned pixels may be undefined. Handle failures and verify captures in the environment where the application will run.
  • Display layout changed: Coordinates depend on the graphics device arrangement. A display reconfiguration can invalidate assumptions made by an existing AWT Robot, so determine the device and coordinate system in the target setup.
  • HiDPI output differs from requested bounds: AWT’s createMultiResolutionScreenCapture can expose a scaled base image and a native-device-resolution variant when a scaling transform applies. JavaFX likewise distinguishes requested dimensions from physical pixel dimensions when scale-to-fit is disabled.
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Or skip the browser setup

If what you need is a screenshot of a web page rather than pixels from the desktop, ScreenshotNeo captures a URL through a single API request. It is a website screenshot API, not a replacement for AWT or JavaFX desktop capture.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

For example, this cURL request saves a page screenshot as WebP:

curl -G "https://api.screenshotneo.com/v1/shot" -d access_key=YOUR_API_KEY --data-urlencode url=https://stripe.com -o shot.webp

See the ScreenshotNeo API documentation for request options. Before capture, it accepts cookie or consent banners like a visitor and removes more than 60 known consent platforms, newsletter popups, and chat widgets; each of those steps can be turned off. Bot checks and CAPTCHAs, blank pages, timeouts, failed loads, and cache hits are not billed, and response headers report the page verdict and billing status. Its MCP server provides screenshot tools for Claude, Cursor, and other MCP clients. The free plan includes 1,000 shots per month with no card; paid plans start at $5 for 3,000 shots. Sign up for the free plan.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.