Source: http://www.study-code.com/java/j2se/66419.htm
Package com. test;
Import java. awt. Dimension;
Import java. awt. Rectangle;
Import java. awt. Robot;
Import java. awt. Toolkit;
Import java. awt. image. BufferedImage;
Import java. io. File;
Import javax. imageio. ImageIO;
/*************************************** ****************************************
* This JavaBean can be called directly in other Java applications to implement "photo" This JavaBean is used to snapshot
* GUI in a Java application! You can embeded it in to your java application
* Source code, and us it to snapshot the right GUI of the application
*
* @ See javax. ImageIO
* @ Author liluqun ([email] liluqun@263.net [/email])
* @ Version 1.0
*
**************************************** **************************************/
Public class GuiCamera {
Private String fileName; // file prefix
Private String defaultName = "GuiCamera ";
Static int serialNum = 0;
Private String imageFormat; // Image File Format
Private String defaultImageFormat = "jpg ";
Dimension d = Toolkit. getdefatooltoolkit (). getScreenSize ();
/*************************************** ************************************
* The default file prefix is GuiCamera. the file format is PNG. The default construct will use the default
* Image file surname "GuiCamera", and default image format "png"
**************************************** **********************************/
Public GuiCamera (){
FileName = defaultName;
ImageFormat = defaultImageFormat;
}
/*************************************** ************************************
* @ Param s
* The surname of the snapshot file
* @ Param format
* The format of the image file, it can be "jpg" or "png"
* This structure supports the storage of JPG and PNG files.
**************************************** **********************************/
Public GuiCamera (String s, String format ){
FileName = s;
ImageFormat = format;
}
/*************************************** ************************************
* SnapShot the Gui once
**************************************** **********************************/
Public void snapShot (){
Try {
// Copy the screen to a BufferedImage object screenshot
BufferedImage screenshot = (new Robot ())
. CreateScreenCapture (new Rectangle (0, 0,
(Int) d. getWidth (), (int) d. getHeight ()));
SerialNum ++;
// Automatically generate a file name based on the file prefix variable and file format variable
String name = fileName + String. valueOf (serialNum) + "."
+ ImageFormat;
File f = new File (name );
System. out. print ("Save File" + name );
// Write the screenshot object to the image file
ImageIO. write (screenshot, imageFormat, f );
System. out. print ("... Finished! \ N ");
} Catch (Exception ex ){
System. out. println (ex );
}
}
Public static void main (String [] args ){
GuiCamera cam = new GuiCamera ("d: \ qq", "bmp ");//
Cam. snapShot ();
}
}