Java Screenshot capture(final String fileName, final Rectangle rect)

Here you can find the source of capture(final String fileName, final Rectangle rect)

Description

capture current window.

License

Open Source License

Parameter

Parameter Description
fileName output file name.
rect rectangle size.

Declaration

public static void capture(final String fileName, final Rectangle rect) 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.awt.AWTException;
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.image.BufferedImage;

import java.io.File;

import java.io.IOException;

import javax.imageio.ImageIO;

public class Main {
    /**//  w  ww .j a  va  2  s. co  m
     * capture current window.
     * @param fileName output file name.
     * @param rect rectangle size.
     */
    public static void capture(final String fileName, final Rectangle rect) {
        final String name = fileName.toLowerCase().endsWith(".png") ? fileName : fileName.concat(".png");
        try {
            final BufferedImage img = new Robot().createScreenCapture(rect);
            ImageIO.write(img, "png", new File(name));
        } catch (final IOException | AWTException e) {
            e.printStackTrace();
        }
    }
}

Related

  1. acquireScreenshot(Component component)
  2. capture()
  3. captureAsScreenshot(final Frame frame)
  4. captureCurrentMonitor()
  5. captureEachMonitor()
  6. captureMainMonitor()