Вы можете использовать следующий код и получить скриншот активированного окна.
Кроме того,
Чтобы получить более качественное изображение окна, настройте следующие переменные как вам нужно
private static int adjustLeft = 6;
private static int fineCutterX = 6;
private static int fineCutterY = 6;
public void getActiveWindowImage() {
char[] buffer = new char[MAX_TITLE_LENGTH * 2];
HWND hwnd = User32.INSTANCE.GetForegroundWindow();
User32.INSTANCE.GetWindowText(hwnd, buffer, MAX_TITLE_LENGTH);
System.out.println("Active window title: " + Native.toString(buffer));
RECT rect = new RECT();
User32.INSTANCE.GetWindowRect(hwnd, rect);
int x = rect.left + adgustLeft;
int y = rect.top;
int width = rect.right - x - fineCutterX;
int hieght = rect.bottom - y - fineCutterY;
System.out.println(x + "," + y + "," + width + "," + hieght);
System.out.println("rect = " + rect);
Robot robot = null;
try {
robot = new Robot();
} catch (AWTException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// Capture the screen shot of the area of the screen defined by the rectangle
BufferedImage bi = robot.createScreenCapture(new Rectangle(x, y, width, hieght));
try {
ImageIO.write(bi, "jpg", new File("<File Path you need to save>"));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}