have implemented robot to take the screenshot of the desktop.

Robot objRobot = null;
                 try
                 {
                    objRobot = new Robot();
                 } catch(Exception ex)
                 {
			
		 }
BufferedImage objBufferedImage =  objRobot.createScreenCapture(objRectArea);

The problem with robot is when my computer is locked the image comes as black.That is what is displayed in the desktop is not
captured.I need the screenshot of what is displayed in the desktop even when my computer is locked.Please suggest me any ideas.
It would be better if it is still possible to do it using robot.

Recommended Answers

All 2 Replies

This has been cross posted on at least 6 different sites, complete with the mis-spelling in the title.
Nobody who codes

} catch(Exception ex)
{

}

deserves an answer anyway.

If you are trying to get the screen of a JFrame, use my example, otherwise ignore it.

As well, I don't think you can get the desktop image if the computer is locked.

Don't use a robot then to get the JFrame image.

I use the following setup:

You will need to import the following:

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;


You will also need to use this code:

Play around with it to see what happens, as well modify it to your uses.

My code is not the mot efficient use of Java, so you may need to research the items in the code some more.

//Define the area we want to save
int areaToExportWidth = 1024;
int areaToExportHeight = 768;

//Create the image 
BufferedImage exportImage = new BufferedImage(areaToExportWidth, areaToExportHeight, BufferedImage.TYPE_INT_ARGB);

//Get graphics - Get the layer we can actually draw on
Graphics2D imageGraphics = (Graphics2D) exportImage.getGraphics();

//Draw Stuff (Images, Boxes, Circles, Whatever)
//Change this part to what you draw on the screen.
imageGraphics.setColor(Color.RED); //Set the color for our drawings
imageGraphics.fillRect(15, 15, 100, 100); //A red square 100x100 15 pixels from the top and left
imageGraphics.setColor(Color.BLACK); //Set a new color for the text
imageGraphics.setFont(new Font("Arial", Font.BOLD, 16)); //Set the font of our text (Arial, Bold, Size 16)
imageGraphics.drawString("This is text", 30, 30); //Put text on image

//Cleanup after ourselves
imageGraphics.dispose();

//Setup to write the BufferedImage to a file
String pathToFile = "C:\\Images";
File outputDirectory = new File(pathToFile);
File outputFile = new File(pathToFile+"\\MyImage.png");
//Here we make sure the directory exists.
/*
 * Returns TRUE if:
 * 	The directory is MISSING
 * 	and/or the directory IS NOT a directory
 */
if(!outputDirectory.exists() || !outputDirectory.isDirectory()){
	outputDirectory.mkdirs(); //Make the directory
} // Else do nothing

//Write the file
try { //Attempt the write
	ImageIO.write(exportImage, "png", outputFile);
} catch (IOException e) { //For some reason it failed so...
	e.printStackTrace(); //... why did it fail?
}

This code produces this:


-- Good luck, make sure to mark other topics (if the exist) on other websites as solved.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.