3

I'm looking for a way to take a screenshot with a Java application of any running directX game. I use the following code

Robot robot = new Robot();  
GraphicsConfiguration config = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().getDefaultConfiguration();  
BufferedImage screenshot = robot.createScreenCapture(config.getBounds());  
ImageIO.write(screenshot,"png", file); 

This one works perfectly anywhere but in directX games. I'm not that familiar with Java and even less with DirectX, I'm just trying to adjust this code. I googled very much, but everything just leads to the same code I already have.

Do you know of another way to take a screenshot of DirectX games?

Thanks.

user68293
  • 53
  • 1
  • 5

1 Answers1

-1

Calling GraphicsDevice#getFullScreenWindow might work, though I haven't really tested this code.

    BufferedImage screenshot;
    GraphicsDevice gd = GraphicsEnvironment
            .getLocalGraphicsEnvironment()
            .getDefaultScreenDevice();
    Window window = gd.getFullScreenWindow();
    if(window == null) {
        Robot robot = new Robot();
        GraphicsConfiguration config = gd.getDefaultConfiguration();
        screenshot = robot.createScreenCapture(config.getBounds()); 
    } else {
        screenshot = new BufferedImage(window.getWidth(),
                window.getHeight(),
                BufferedImage.TYPE_INT_RGB);
        window.paint(screenshot.getGraphics());
    }
    ImageIO.write(screenshot, "png", file);
Lone nebula
  • 4,768
  • 2
  • 16
  • 16
  • Thanks for your answer, but problem is, that `window` is somehow still `null` after calling `getFullScreenWindow()`. – user68293 Sep 21 '14 at 21:31
  • @user68293 I couldn't find anything on the internet explaining how to do it in Java. Maybe the accepted answer [here](http://stackoverflow.com/questions/1962142/take-screenshot-of-directx-full-screen-application#1962286) would be an alternative approach. – Lone nebula Sep 24 '14 at 12:12