java print screen two monitors
Asked Answered
S

3

13

I'm trying to use print screen image area to get 2 monitors, but only works for one monitor. Can you advise me how to get figure 2 monitors?

        Robot robot = new Robot();    
        Rectangle screenRect = new Rectangle(Toolkit.getDefaultToolkit().getScreenSize());
        BufferedImage capture = new Robot().createScreenCapture(screenRect);
        ImageIO.write(capture, "bmp", new File("printscreen.bmp"));
Strabismus answered 18/8, 2013 at 16:46 Comment(0)
H
22

Union together the bounds of each screen:

Rectangle screenRect = new Rectangle(0, 0, 0, 0);
for (GraphicsDevice gd : GraphicsEnvironment.getLocalGraphicsEnvironment().getScreenDevices()) {
    screenRect = screenRect.union(gd.getDefaultConfiguration().getBounds());
}
BufferedImage capture = new Robot().createScreenCapture(screenRect);
Heteropterous answered 18/8, 2013 at 17:32 Comment(1)
how about if we just need only the second screen....? can we do intersect ? what about the code....Groat
C
3

You could try:

int width = 0;
int height = 0;

GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
GraphicsDevice[] gs = ge.getScreenDevices();

for (GraphicsDevice curGs : gs)
{
  DisplayMode mode = curGs.getDisplayMode();
  width += mode.getWidth();
  height = mode.getHeight();
}

This should calculate the total width of multiple screens. Obviously it only supports horizontally aligned screens in the form above - you'd have to analyse the bounds of the graphics configurations to handle other monitor alignments (depends how bulletproof you want to make it).

If your Main Monitor is on the right side and you want to get an image even with the left side, use this:

Rectangle screenRect = new Rectangle(-(width / 2), 0, width, height);
BufferedImage capture = new Robot().createScreenCapture(screenRect);
ImageIO.write(capture, "bmp", new File("printscreen.bmp"));
Cloudland answered 18/8, 2013 at 16:49 Comment(5)
Thank you for the reply, I tried and it works, but the problem is that:Program take a picture of the main monitor and monitor on the right (BUT I have second monitor on the left)Strabismus
As far as I can understand you pass the rectangle size if your screen, given by the toolkit method get screen size. What about trying a bigger size?? Maybe it works..Marrissa
@Marrissa the problem is not the size, the problem is the position because default its 0,0 but this is on the right screen, the left screen is ignoredCloudland
How nice, sorry i didn't read the edited part.. Should be working, but what if the two monitors are not equal in length? Does it keep holding??Marrissa
@Marrissa thats a good comment, yes the actual code will not cover this, but it should be easy to save the width of the first monitorCloudland
G
1

This is the code I have used and tested , it works , it creates two png files inside the res folder(change it to your folder) one for my primary and the other for the secondary screen . i had also printed the Bounds information about the Displays , if you want both displays in one image , just add the width of both monitors and you will have it

public static void screenMultipleMonitors() {
    GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
    GraphicsDevice[] gDevs = ge.getScreenDevices();

    for (GraphicsDevice gDev : gDevs) {
        DisplayMode mode = gDev.getDisplayMode();
        Rectangle bounds = gDev.getDefaultConfiguration().getBounds();
        System.out.println(gDev.getIDstring());
        System.out.println("Min : (" + bounds.getMinX() + "," + bounds.getMinY() + ") ;Max : (" + bounds.getMaxX()
                + "," + bounds.getMaxY() + ")");
        System.out.println("Width : " + mode.getWidth() + " ; Height :" + mode.getHeight());

        try {
            Robot robot = new Robot();

            BufferedImage image = robot.createScreenCapture(new Rectangle((int) bounds.getMinX(),
                    (int) bounds.getMinY(), (int) bounds.getWidth(), (int) bounds.getHeight()));
            ImageIO.write(image, "png",
                    new File("src/res/screen_" + gDev.getIDstring().replace("\\", "") + ".png"));

        } catch (AWTException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}
Galvanometer answered 19/6, 2016 at 3:8 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.