这里指定输出到C盘下面的1.bmp
另外 ImageIO.write(image, "bmp", fo); 的第二个参数指定类型为bmp,第三个参数指定要输出的流
import javax.imageio.ImageIO;
import java.io.*;
import java.awt.*;
import java.awt.image.BufferedImage;
public class Test {
public static void main(String[] args) throws AWTException, FileNotFoundException{
GraphicsEnvironment environment = GraphicsEnvironment.getLocalGraphicsEnvironment();
GraphicsDevice screen = environment.getDefaultScreenDevice();
Robot robot=new Robot(screen);
Dimension scrSize = Toolkit.getDefaultToolkit().getScreenSize(); // 桌面屏幕尺寸
Rectangle r=new Rectangle(0, 0, scrSize.width, scrSize.height);
//Rectangle r1=new Rectangle(0,0,20,30);
BufferedImage image=robot.createScreenCapture(r);
FileOutputStream fo=new FileOutputStream("c://1.bmp");
try {
ImageIO.write(image, "bmp", fo);
fo.flush();
} catch (IOException e) {
e.printStackTrace();
}
}
}