Reputation: 13
I am currently making a fullscreen game and I want to be able to take some screenshots. I have found this little code snippet:
BufferedImage image =
new Robot().createScreenCapture(new Rectangle(w.getX(), w.getY(),w.getWidth(),w.getHeight()));
ImageIO.write(image, "jpg", new File("C:\\Users\\Kaizer\\Desktop\\", "ScreenShot" + counter + ".jpg"));
I know it doesnt look pretty, but it makes a screenshot, but from my desktop, and not my actual fullscreen game. The Windows screenshots does the same.
I know there is something I overlooked, but I can't figure it out for the life of me.
By the way: this code is run when the player presses the F11 button. It is not its own method.
Upvotes: 1
Views: 619
Reputation: 44808
If you wrote your game with Swing or AWT, instead of using Robot
, you can do the following:
private static void captureImage(Component c, String fileName)
throws IOException {
BufferedImage img = new BufferedImage(c.getWidth(), c.getHeight(),
BufferedImage.TYPE_INT_RGB);
c.paint(img.getGraphics());
ImageIO.write(img, "jpg", new File(fileName + ".jpg"));
}
Note: Depending on the Component
you're using, it may or may not need to be visible. But seeing as you're taking a picture from a game, I'm pretty sure that won't matter.
Upvotes: 1
Reputation: 168825
from my desktop, and not my actual fullscreen game..
This is typical of the Robot
(or standard OS screenshot software) when a game does active rendering. AFAIU there is nothing that can be done about it (beyond - grab your screenshots with your phone camera).
Upvotes: 0