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"));
Also, using Ctrl + PrtSc will not take screenshots of a single screen. Well, you don’t need to scratch your head. The following steps will help you with how to print one screen with dual monitors using two easy keyboard shortcuts: Firstly, drag your mouse pointer to the monitor you wish to print.
Java Program to Print Screen Resolution 1 Dimension size = Toolkit.getDefaultToolkit ().getScreenSize (); 2 getScreenSize will return the size in pixel. 3 If directly size is printed, then it is displayed in the format: java.awt.Dimension [width=1366, height=768]
Capture full screen Java example To capture a screenshot of the whole screen, we need to know the screen size. The following statement obtains the screen size as a Rectangle object: * as an image which will be saved into a file. String fileName = "FullScreenshot." + format; System.out.println ("A full screenshot saved!");
In some cases, you decide to take a screenshot of only one monitor instead of snipping the two at the same time. What will you do? You may have found the Print Screen button is unhelpful since it keeps capturing all screens. On this page, I will show you 2 methods about how to take a screenshot of only one monitor.
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();
}
}
}
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With