Is there a way to take a screenshot using Java and save it to some sort of image?

后端 未结 8 710
盖世英雄少女心
盖世英雄少女心 2020-11-22 06:19

Simple as the title states: Can you use only Java commands to take a screenshot and save it? Or, do I need to use an OS specific program to take the screenshot and then gra

8条回答
  •  隐瞒了意图╮
    2020-11-22 07:03

    Toolkit returns pixels based on PPI, as a result, a screenshot is not created for the entire screen when using PPI> 100% in Windows. I propose to do this:

    DisplayMode displayMode = GraphicsEnvironment.getLocalGraphicsEnvironment().getScreenDevices()[0].getDisplayMode();
    Rectangle screenRectangle = new Rectangle(displayMode.getWidth(), displayMode.getHeight());
    BufferedImage screenShot = new Robot().createScreenCapture(screenRectangle);
    

提交回复
热议问题