package com; import java.awt.AWTException; import java.awt.Dimension; import java.awt.Rectangle; import java.awt.Toolkit; import java.awt.image.BufferedImage; import java.io.File; import java.io.IOException; import javax.imageio.ImageIO; public class ScreenCapture { public static void main(String[] args) throws AWTException, IOException { Dimension d = Toolkit.getDefaultToolkit().getScreenSize(); //拷贝屏幕到一个BufferedImage对象screenshot BufferedImage bi = (new java.awt.Robot()).createScreenCapture(new Rectangle(0,0,(int)d.getWidth(),(int)d.getHeight())); //将screenshot对象写入图像文件 ImageIO.write(bi, "JPG", new File("d://desk.jpg")); /* Dimension d = Toolkit.getDefaultToolkit().getScreenSize(); BufferedImage screenshot = (new Robot()).createScreenCapture(new Rectangle(0,0,(int)d.getWidth(),(int)d.getHeight())) ; OutputStream out = new BufferedOutputStream(new FileOutputStream ("d:/desk.jpg")) ; JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder (out) ; encoder.encode(screenshot) ; out.close() ; */ } }