Java截图:如何使用Java进行屏幕截图

・1 分钟阅读

/*
 * Take a snapshot of the screen using the Java Robot class.
 * Return the screen shot as an Image (BufferedImage).
 *
 */
public BufferedImage getBackgroundImage()
{
 try {
 Robot rbt = new Robot();
 Toolkit tk = Toolkit.getDefaultToolkit();
 Dimension dim = tk.getScreenSize();
 BufferedImage background = rbt.createScreenCapture(new Rectangle(0, yOffset - 1, (int) dim.getWidth(), (int) dim.getHeight()));
 return background;
 //return new ImageIcon(background);
 }
 catch (Exception ex) {
 ex.printStackTrace();
 }
}

值得注意的是,当我的应用程序不可见时,我实际上进行了截图,我会隐藏主JFrame,然后截取屏幕的图片,然后显示我的JFrame。

Xn_warm profile image