1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Screenshot capturing and saving in local System

Discussion in 'Java' started by sameer_havakajoka, Nov 19, 2009.

  1. sameer_havakajoka

    sameer_havakajoka New Member

    Joined:
    Sep 14, 2009
    Messages:
    271
    Likes Received:
    2
    Trophy Points:
    0
    Occupation:
    Sleeping
    Location:
    Hava Ke Paro Me

    Introduction



    This Code snippet is used to capture Screenshot automatically and save it.

    The below code performs the function of taking screenshots automatically making use of the class java.awt.Robot .The package java.awt contains all of the classes for creating user interfaces and for painting graphics and images.

    The class java.awt.Toolkit helps in obtaining the screen size of the page and the class java.awt.Rectangle creates a rectangle with the same dimensions of the page.

    The class java.awt.image.BufferedImage helps in creating a image containing pixels and colour similar to page and this does not involve the mouse cursor.

    The taken screen shot can be stored in any mode depending on user's preference.

    Thus this code automates the procedure of obtaining screenshots so the user may find it easier to know the status of the page .

    The code



    Code:
    package businessFunctions;
    
    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;
    
    public class Screenshot 
    {
    	public void captureScreen(String nameOfScreenshotFile, String formatOfFile, String screenshotPath) throws Exception 
    	{
    		/*Dimension obj helps in adjusting the width and height of the file */
    
    		Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
    		/*create a rectangle starting from (0,0)(top,left) to the width and height in the dimension obj */
    
    		Rectangle screenRectangle = new Rectangle(screenSize);
    		Robot robot = new Robot(); 
    		BufferedImage image = robot.createScreenCapture(screenRectangle); 
    		
    		/*Creates an image containing pixels read from the screen. This image does not include the mouse cursor.*/ 
    		 
    		ImageIO.write(image, formatOfFile, new File(screenshotPath+nameOfScreenshotFile+"."+formatOfFile));
    	}
    	 
    	public static void main(String args[]) 
    	{
    		Screenshot obj=new Screenshot();
    		try 
    		{
    			obj.captureScreen("trial","jpeg","C:\\Documents and Settings\\sameer\\My Documents\\EclipseProjects\\Screenshot\\DEST\\");
    		} 
    		catch (Exception e) 
    		{
    			e.printStackTrace();
    		}
    	}
    }
     
  2. rasd123

    rasd123 Banned

    Joined:
    Nov 4, 2009
    Messages:
    40
    Likes Received:
    0
    Trophy Points:
    0
    Thanks for this site very helpful.
     
  3. sameer_havakajoka

    sameer_havakajoka New Member

    Joined:
    Sep 14, 2009
    Messages:
    271
    Likes Received:
    2
    Trophy Points:
    0
    Occupation:
    Sleeping
    Location:
    Hava Ke Paro Me
    welcome, and site is of admin
     
  4. rasd123

    rasd123 Banned

    Joined:
    Nov 4, 2009
    Messages:
    40
    Likes Received:
    0
    Trophy Points:
    0
    Hello, this is great article. I have blog and I thanks to say you thanks. Regards!
     
  5. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,285
    Likes Received:
    364
    Trophy Points:
    83
  6. rasd123

    rasd123 Banned

    Joined:
    Nov 4, 2009
    Messages:
    40
    Likes Received:
    0
    Trophy Points:
    0
    I like this's, cool!
     
  7. technica

    technica New Member

    Joined:
    Dec 15, 2007
    Messages:
    107
    Likes Received:
    0
    Trophy Points:
    0
    Home Page:
    Good one. Thanks for sharing it with us all
     
  8. rasd123

    rasd123 Banned

    Joined:
    Nov 4, 2009
    Messages:
    40
    Likes Received:
    0
    Trophy Points:
    0
    HaHA! I have already seen the great thing!!!
     

Share This Page