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.

Dani AI

Generated

As observed, Robot.createScreenCapture commonly returns a black image when the machine is locked. This is expected behavior: the OS switches to a separate, protected desktop (Windows uses the Winlogon/LogonUI "secure desktop"; modern Wayland compositors and macOS apply similar protections). A Java Robot runs in the interactive user session and cannot read that protected framebuffer, so there is no reliable, pure‑Java way to capture what the physical monitor is showing once the workstation is locked.

Notes tied to the existing replies:

  • : do not swallow exceptions—log AWTException/Runtime exceptions from new Robot() and createScreenCapture() so you see failures instead of hiding them.
  • : rendering your app into a BufferedImage and saving that works if you control the content (it avoids OS capture entirely), but it does not help capture the OS lock screen or other apps.

Practical alternatives and tradeoffs:

  • Capture before lock: detect session lock/unlock (Windows: WTS session notifications via JNA/JNI) and take a final screenshot just before the lock event.
  • Hardware capture: an external HDMI/DVI/DisplayPort capture device (splitter + capture card) records the video output regardless of OS lock state.
  • Privileged drivers/remote‑control tools: mirror drivers or signed kernel hooks used by some remote‑control products can capture the console when locked, but they require admin rights, driver signing, and have security/legal implications.
  • Rethink requirements: if you only need your app's output, render and save it yourself (as suggested).

Quick diagnostic tip (detect a black capture):

boolean probablyBlack(BufferedImage img) {
  int w = img.getWidth(), h = img.getHeight();
  for (int y = 0; y < h; y += Math.max(1, h/10))
    for (int x = 0; x < w; x += Math.max(1, w/10))
      if ((img.getRGB(x,y) & 0xFFFFFF) != 0) return false;
  return true;
}

Security/legal caution: bypassing OS protections is intrusive; any solution that captures a locked session must respect user consent, company policy, and applicable law.

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.