简体   繁体   中英

ScreenShot solution takes a screentshot of my desktop

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.

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.

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).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM