Skip to content

Instantly share code, notes, and snippets.

@obullxl
Created October 11, 2013 05:17
Show Gist options
  • Save obullxl/6929915 to your computer and use it in GitHub Desktop.
Save obullxl/6929915 to your computer and use it in GitHub Desktop.
Java 截屏代码 截取电脑屏幕 并保存为图片
import java.awt.AWTException;
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
public class ScreenSnapshot {
public static void main(String[] args) {
try {
int width = (int)Toolkit.getDefaultToolkit().getScreenSize().getWidth(); //要截取的宽度
int height = (int)Toolkit.getDefaultToolkit().getScreenSize().getHeight(); //要截取的高度
Robot robot = new Robot();
BufferedImage image = robot.createScreenCapture(new Rectangle(width,height));
image = image.getSubimage(0, 0, 200, 500);
ImageIO.write (image, "png" , new File("c:/1.png")); //保存在C盘 图片名为1.png
} catch (AWTException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
@obullxl
Copy link
Author

obullxl commented Oct 11, 2013

image = image.getSubimage(0, 0, width, height);
截取整个桌面图片。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment