java print screen two monitors

前端 未结 3 610
遇见更好的自我
遇见更好的自我 2021-02-13 20:03

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         


        
3条回答
  •  隐瞒了意图╮
    2021-02-13 20:44

    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"));
    

提交回复
热议问题