Monday, February 21, 2011

Java multi-monitor screenshots

Random snippets I might want again one day ;)

Create a "C:\\CanDelete" directory before running any of this, or change the path building.

Take a screenshot of each monitor individually:
package com.blogspot.whileonefork.screencapture;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.image.BufferedImage;
import java.io.File;

import javax.imageio.ImageIO;


public class EachMonitor {
 public static void main(String[] argv) throws Exception {
  //Take a screenshot of every monitor
  GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
  GraphicsDevice[] screens = ge.getScreenDevices();
  
  for (GraphicsDevice screen : screens) {
   Robot robotForScreen = new Robot(screen);
   Rectangle screenBounds = screen.getDefaultConfiguration().getBounds();
   
   //The screen bounds have an annoying tendency to have an offset x/y; we want (0,0) => (width, height)
   screenBounds.x = 0;
   screenBounds.y = 0;
   BufferedImage screenShot = robotForScreen.createScreenCapture(screenBounds);
   String ext = "jpg";
   String outputFile = "C:\\CanDelete\\screenshot_" 
    + screen.getIDstring().replaceAll("[^A-Za-z0-9]", "_")
    + "."
    + ext;   
   ImageIO.write(screenShot, ext, new File(outputFile));
   System.out.println("Saved " + outputFile 
    + " of " + screen.getIDstring() 
    + " xy=(" + screenBounds.x + "," + screenBounds.y + ")"
    + " bounds=("+ screenBounds.width + "," + screenBounds.height + ")");
  }
 }
}


Take a single big screenshot of all monitors:

package com.blogspot.whileonefork.screencapture;

import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.image.BufferedImage;
import java.io.File;

import javax.imageio.ImageIO;

public class AllMonitors {
 public static void main(String[] argv) throws Exception {      
  /**
   * Take one big screenshot of the entire UI. 
   * On my Windows box monitors seem to act like they are side by side on X, extending on Y.
   * Seems, at least on windows, to ignore any offset config setup in display properties.
   */
  GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
  GraphicsDevice[] screens = ge.getScreenDevices();
  
  Rectangle allScreenBounds = new Rectangle();
  for (GraphicsDevice screen : screens) {
   Rectangle screenBounds = screen.getDefaultConfiguration().getBounds();
   
   allScreenBounds.width += screenBounds.width;
   allScreenBounds.height = Math.max(allScreenBounds.height, screenBounds.height);      
  }
  
  Robot robot = new Robot();
  BufferedImage screenShot = robot.createScreenCapture(allScreenBounds);
  
  String ext = "jpg";
  String outputFile = "C:\\CanDelete\\AllMonitors" 
   + "."
   + ext;   
  ImageIO.write(screenShot, ext, new File(outputFile));
  System.out.println("Saved " + outputFile 
   + " of all monitors " 
   + " xy=(" + allScreenBounds.x + "," + allScreenBounds.y + ")"
   + " bounds=("+ allScreenBounds.width + "," + allScreenBounds.height + ")");  
 } 
}


All this has been tested exactly once on a Windows box with two monitors of substantially different size; ymmv :)

75 comments:

Dustin said...

This seems to give errors when the first monitor is lower than the following monitors. If anyone else runs into this problem a corrected version can be found below. Also note that this version does not save a file, and instead returns a buffered image.


package book;

import java.awt.AWTException;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;
import java.util.logging.Level;
import java.util.logging.Logger;

public class AllMonitors {

public BufferedImage getScreenshot() {

//get handle to graphics environment
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
GraphicsDevice[] screens = ge.getScreenDevices();

//if screens are detected proceed
if (screens.length > 0) {

try {
//get the first screen
GraphicsDevice a = screens[0];

//get the monitor resolution
Toolkit t = Toolkit.getDefaultToolkit();
Rectangle allScreenBounds = new Rectangle(t.getScreenSize());

//offset screen capture (in cases where first monitor is not the
//highest this will come into effect)
allScreenBounds.y = -1 * screens[0].getDefaultConfiguration().getBounds().y;

//take screenshot
Robot robot = new Robot();
BufferedImage screenShot = robot.createScreenCapture(allScreenBounds);

return screenShot;
} catch (AWTException ex) {
Logger.getLogger(AllMonitors.class.getName()).log(Level.SEVERE, null, ex);
}
}

//if no screens detected return null
return null;
}
}

Unknown said...

The code to compute the virtual screen bounds can be found in the jdk's javadoc (http://docs.oracle.com/javase/1.4.2/docs/api/java/awt/GraphicsConfiguration.html):

Rectangle virtualBounds = new Rectangle();
GraphicsEnvironment ge = GraphicsEnvironment.
getLocalGraphicsEnvironment();
GraphicsDevice[] gs =
ge.getScreenDevices();
for (int j = 0; j < gs.length; j++) {
GraphicsDevice gd = gs[j];
GraphicsConfiguration[] gc =
gd.getConfigurations();
for (int i=0; i < gc.length; i++) {
virtualBounds =
virtualBounds.union(gc[i].getBounds());
}
}

Scott Schafer said...

Thanks for this post, but especially +1000 to Dustin. If I had hair, I would have pulled it all out over this bug. Your comment saved me a lot of aggravation.

Unknown said...

add this to the end of the for loop and it will work with as many monitors as you have ::: allScreenBounds.x=Math.min(allScreenBounds.x, screenBounds.x);
allScreenBounds.y=Math.min(allScreenBounds.y, screenBounds.y);

Unknown said...

add this to the end of the for loop and it will work with as many monitors as you have ::: allScreenBounds.x=Math.min(allScreenBounds.x, screenBounds.x);
allScreenBounds.y=Math.min(allScreenBounds.y, screenBounds.y);

for ict 99 said...

I have read your blog its very attractive and impressive. I like it your blog.

Java Training in Chennai Core Java Training in Chennai Core Java Training in Chennai

Java Online Training Java Online Training Core Java 8 Training in Chennai Core java 8 online training JavaEE Training in Chennai Java EE Training in Chennai

navya said...

I have read your blog its very attractive and impressive. I like it your blog.

Java Online Training Java EE Online Training Java EE Online Training Java 8 online training Core Java 8 online training

Java Online Training from India Java Online Training from India Core Java Training Online Core Java Training Online Java Training InstitutesJava Training Institutes

naveed said...

Thanks for sharing the info, keep up the good work going.... I really enjoyed exploring your site. good resource...best modem router combo

Yogayogi said...

Great post! I am actually getting ready to across this information, It’s very helpful for this blog.Also great with all of the valuable information you have Keep up the good work you are doing well.
Data Science training in Chennai
Data science online training

priya said...

I appreciate that you produced this wonderful article to help us get more knowledge about this topic. I know, it is not an easy task to write such a big article in one day, I've tried that and I've failed. But, here you are, trying the big task and finishing it off and getting good comments and ratings. That is one hell of a job done!

Data Science course in Chennai
Data science course in bangalore
Data science course in pune
Data science online course
Data Science Interview questions and answers
Data Science Tutorial

rohini said...

Nice tutorial. Thanks for sharing the valuable information. it’s really helpful. Who want to learn this blog most helpful. Keep sharing on updated tutorials…
Best Devops Training in pune
Devops Training in Bangalore
Microsoft azure training in Bangalore
Power bi training in Chennai

sheikhali said...

nice one...!
t shirt printing in perth

Deepak said...

Amazon Quiz Answers Today
Deals Ki Dunia

datasciencecourse said...

data science course bangalore is the best data science course

svrtechnologies said...

Thanks for Sharing Such an useful & Informative Stuff...

data science tutorial

Unknown said...

I feel very grateful that I read this. It is very helpful and very informative and I really learned a lot from it.
data science course

datasciencecourse said...

Excellent Blog! I would like to thank for the efforts you have made in writing this post. I am hoping the same best work from you in the future as well. I wanted to thank you for this websites! Thanks for sharing. Great websites!
courses in business analytics

data science course in mumbai

data analytics courses

data science interview questions

digitaltucr said...


I finally found great post here.I will get back here. I just added your blog to my bookmark sites. thanks.Quality posts is the crucial to invite the visitors to visit the web page, that's what this web page is providing.
data analytics courses
ExcelR Data Science training in Mumbai
data science interview questions
ExcelR Business Analytics courses in Mumbai

Priyanka said...

Attend The Business Analytics Courses From ExcelR. Practical Business Analytics Courses Sessions With Assured Placement Support From Experienced Faculty. ExcelR Offers The Data Analytics Courses.
ExcelR Business Analytics Courses
Data Science Interview Questions

Ecare Technologies said...

I have read your blog its very attractive and impressive. I like it your blog...
Data Science Training in Bangalore
Data Science Courses in Bangalore

datasciencecourse said...

I like viewing web sites which comprehend the price of delivering the excellent useful resource free of charge. I truly adored reading your posting. Thank you!

Digital marketing course mumbai

offpageseo70 said...

Nice blog,I understood the topic very clearly,And want to study more like this.
Data Scientist Course

python training in vijayawada said...

This is a wonderful article, Given so much info in it, Thanks for sharing. CodeGnan offers courses in new technologies and makes sure students understand the flow of work from each and every perspective in a Real-Time environmen python training in vijayawada. , data scince training in vijayawada . , java training in vijayawada. ,

python training in vijayawada said...

This is a wonderful article, Given so much info in it, Thanks for sharing. CodeGnan offers courses in new technologies and makes sure students understand the flow of work from each and every perspective in a Real-Time environmen python training in vijayawada. , data scince training in vijayawada . , java training in vijayawada. ,

Data Science Course said...

Impressive! I finally found great post here. Nice article on data science . It's really a nice experience to read your post. Thanks for sharing your innovative ideas to our vision.
Data Science Course
Data Science Course in Marathahalli

Unknown said...

Thanks for sharing the info, keep up the good work going....You may have to try get clash of magic apk

j8899 said...


Great post i must say and thanks for the information. Education is definitely a sticky subject. However, is still among the leading topics of our time. I appreciate your post and look forward to more.

digital marketing courses mumbai

priyash said...

wonderful article. Very interesting to read this article.I would like to thank you for the efforts you had made for writing this awesome article. This article resolved my all queries.
Data science Interview Questions
Data Science Course

priyash said...

wonderful article. Very interesting to read this article.I would like to thank you for the efforts you had made for writing this awesome article. This article resolved my all queries. keep it up.
data analytics course in Bangalore

Anebellyliza said...

Nice Blog ! It was really a nice article and i was really impressed by reading this. Thanks for sharing such detailed information.
Data Science Training in Hyderabad

datasciencecourse said...

Great post i must say and thanks for the information. Education is definitely a sticky subject. However, is still among the leading topics of our time. I appreciate your post and look forward to more.

Correlation vs Covariance

Avijit said...

I normally wouldn't be so engaged by any articles pertaining to this subject, but yours grabbed my attention. It was like a great dessert crying out to me to eat it. This is good content.

SEO Services in Kolkata
Best SEO Services in Kolkata
SEO Company in Kolkata
Best SEO Company in Kolkata
Top SEO Company in Kolkata
Top SEO Services in Kolkata
SEO Services in India
SEO Company in India

eazyclasses said...

Very few authors can convince me in their mind. You've worked superbly of doing that on a large number of your perspectives here.


Online Teaching Platforms
Online Live Class Platform
Online Classroom Platforms
Online Training Platforms
Online Class Software
Virtual Classroom Software
Online Classroom Software
Learning Management System
Learning Management System for Schools
Learning Management System for Colleges
Learning Management System for Universities

priyash said...

Very interesting to read this article.I would like to thank you for the efforts you had made for writing this awesome article. This article inspired me to read more. keep it up.
Correlation vs Covariance
Simple linear regression

BestTrainingKolkata said...

This article contains some of the most informative content I've read in quite some time. The points of this content are clear-cut and engaging. I think much like this writer.
SAP training in Kolkata
Best SAP training in Kolkata
SAP training institute in Kolkata

BestTrainingMumbai said...

I am speechless as to how great this article is. The content is simple to understand and very engaging. This is by far some of the best content I've seen.
SAP training in Mumbai
Best SAP training in Mumbai
SAP training institute Mumbai

CloudLearn ERP said...

Your article is extremely well-written. This is great informational content from my point of view. You also make many valid points with compelling, unique content.
Best Data Science training in Mumbai

Data Science training in Mumbai

lionelmessi said...


Extraordinary Information Thanks For Sharing With Us

Data Science Training in Hyderabad
Data Science Course in Hyderabad

Avijit said...

There are numerous parts of this article on which I agree with you. You have created neural connections in my mind not utilized frequently. Much thanks to you for getting my neurons bouncing.


Denial management software
Denials management software
Hospital denial management software
Self Pay Medicaid Insurance Discovery
Uninsured Medicaid Insurance Discovery
Medical billing Denial Management Software
Self Pay to Medicaid
Charity Care Software
Patient Payment Estimator
Underpayment Analyzer
Claim Status

Dubai Dandb said...

Very interesting blog. Many blogs I see these days do not really provide anything that attracts others, but believe me the way you interact is literally awesome.You can also check my articles as well.

Data Science In Banglore With Placements
Data Science Course In Bangalore
Data Science Training In Bangalore
Best Data Science Courses In Bangalore
Data Science Institute In Bangalore

Thank you..

priyash said...

Very interesting to read this article.I would like to thank you for the efforts you had made for writing this awesome article. This article inspired me to read more. keep it up.
Correlation vs Covariance
Simple linear regression
data science interview questions

Anirban Ghosh said...

Wow! This article is jam-packed full of useful information. The points made here are clear, concise, readable and poignant. I honestly like your writing style.
SAP training in Kolkata
SAP training Kolkata
Best SAP training in Kolkata
SAP course in Kolkata
SAP training institute Kolkata

nisharoshan said...

Well Said, you have furnished the right information that will be useful to anyone at all time. Thanks for sharing your Ideas.

Web Designing Training Course in Chennai | Certification | Online Course Training | Web Designing Training Course in Bangalore | Certification | Online Course Training | Web Designing Training Course in Hyderabad | Certification | Online Course Training | Web Designing Training Course in Coimbatore | Certification | Online Course Training | Web Designing Training Course in Online | Certification | Online Course Training





lionelmessi said...

Excellent article useful to all the aspirants.

DevOps Course Training in Hyderabad
Best DevOps Course Training in Hyderabad

devi said...

Excellent blog with lots of information, keep sharing. I am waiting for your more posts like this or related to any other informative topic.
Data Science Training In Chennai | Certification | Data Science Courses in Chennai | Data Science Training In Bangalore | Certification | Data Science Courses in Bangalore | Data Science Training In Hyderabad | Certification | Data Science Courses in hyderabad | Data Science Training In Coimbatore | Certification | Data Science Courses in Coimbatore | Data Science Training | Certification | Data Science Online Training Course

Priyanka said...

Attend The Data Analyst Course From ExcelR. Practical Data Analyst Course Sessions With Assured Placement Support From Experienced Faculty. ExcelR Offers The Data Analyst Course.
Data Analyst Course

Anonymous said...

Thank you for this informative blog
python training in bangalore | python online training
artificial intelligence training in bangalore | artificial intelligence online training
machine learning training in bangalore | machine learning online training
blockchain training in bangalore | blockchain online training
uipath training in bangalore | uipath online training

Priyanka said...

Attend The Course in Data Analytics From ExcelR. Practical Course in Data Analytics Sessions With Assured Placement Support From Experienced Faculty. ExcelR Offers The Course in Data Analytics.
Course in Data Analytics

Anonymous said...

Thanks you for sharing information.
python training in bangalore | python online trainng
artificial intelligence training in bangalore | artificial intelligence online training
uipath training in bangalore | uipath online training
blockchain training in bangalore | blockchain online training

EXCELR said...


Very interesting blog Thank you for sharing such a nice and interesting blog and really very helpful article.
Data Science Course in Hyderabad

priyanka said...

Very interesting to read this article.I would like to thank you for the efforts you had made for writing this awesome article. This article inspired me to read more. keep it up.
Correlation vs Covariance
Simple linear regression
data science interview questions

datasciencecourse said...

Cool stuff you have and you keep overhaul every one of us

data science interview questions

Priyanka said...

Attend The Data Analyst Course From ExcelR. Practical Data Analyst Course Sessions With Assured Placement Support From Experienced Faculty. ExcelR Offers The Data Analyst Course.
Data Analyst Course

Theverge said...

https://digitalweekday.com/
https://digitalweekday.com/
https://digitalweekday.com/
https://digitalweekday.com/
https://digitalweekday.com/
https://digitalweekday.com/
https://digitalweekday.com/
https://digitalweekday.com/

Theverge said...

https://digitalweekday.com/
https://digitalweekday.com/
https://digitalweekday.com/
https://digitalweekday.com/
https://digitalweekday.com/
https://digitalweekday.com/
https://digitalweekday.com/
https://digitalweekday.com/

lionelmessi said...

This Information Which You Shared Was Really Fantastic
Hadoop Training in Hyderabad

Hadoop Course Training Institute in Hyderabad

datasciencecourse said...

I am looking for and I love to post a comment that "The content of your post is awesome" Great work!

data science interview questions

datasciencecourse said...

I am looking for and I love to post a comment that "The content of your post is awesome" Great work!

Simple Linear Regression

Correlation vs covariance

KNN Algorithm

priyash said...

Amazing Article ! I would like to thank you for the efforts you had made for writing this awesome article. This article inspired me to read more. keep it up.
Correlation vs Covariance
Simple Linear Regression
data science interview questions
KNN Algorithm
Logistic Regression explained

datasciencecourse said...

I am looking for and I love to post a comment that "The content of your post is awesome" Great work!

Simple Linear Regression

Correlation vs Covariance

Unknown said...

Great Article Artificial Intelligence Projects

Project Center in Chennai

JavaScript Training in Chennai

JavaScript Training in Chennai

Rohini said...

It’s very informative and you are obviously very knowledgeable in this area. You have opened my eyes to varying views on this topic with interesting and solid content.
Data Analyst Course

priyanka said...

Amazing Article ! I would like to thank you for the efforts you had made for writing this awesome article. This article inspired me to read more. keep it up.
Simple Linear Regression
Correlation vs covariance
data science interview questions
KNN Algorithm
Logistic Regression explained

Priyanka said...

Attend The Data Analyst Course From ExcelR. Practical Data Analyst Course Sessions With Assured Placement Support From Experienced Faculty. ExcelR Offers The Data Analyst Course.
Data Analyst Course

BEST SEO SERVICES said...

Much obliged for this post, yet particularly +1000 to Dustin. In the event that I had hair, I would have hauled everything out over this bug. Your remark spared me a great deal of disturbance....
rooftop snipers

priyanka said...

very well explained. I would like to thank you for the efforts you had made for writing this awesome article. This article inspired me to read more. keep it up.
Logistic Regression explained
Correlation vs Covariance
Simple Linear Regression
data science interview questions
KNN Algorithm

regina filangee said...

Good site you have here.. It’s hard to find quality writing like yours nowadays. I seriously appreciate people like you! If you are gaming enthusiast and is looking for the best vertical monitors, you can always visit us . We have made a list of some of the best monitors with the best connections likecheap HDMI monitors and best monitors for developersthat are apt for programming and coding and best monitors for graphic designing.

Insta Follower said...

Having read this I believed it was really informative. I appreciate you taking the time and effort to put this informative article together. I once again find myself spending a lot of time both reading and posting comments. But so what, it was still worth it!

Buy Instagram Likes $1

Gopinath N said...

Hi! Thank you for the share this information. This is very useful information for online blog review readers. Keep it up such a nice posting like this.

Data Science Training in Chennai

Data Science Course in Chennai

Mallela said...

Thanks for posting the best information and the blog is very helpful.Data science course in Faridabad

Data Scientist said...

Highly appreciable regarding the uniqueness of the content. This perhaps makes the readers feels excited to get stick to the subject. Certainly, the learners would thank the blogger to come up with the innovative content which keeps the readers to be up to date to stand by the competition. Once again nice blog keep it up and keep sharing the content as always.
Data Science Training

Mallela said...

Thanks for posting the best information and the blog is very helpful.data science interview questions and answers

Deekshitha said...

Informative blog
Data Science Course in India

Maneesha said...

I would like to thank you for the efforts you have made in writing this article. I am hoping the same best work from you in the future as well..
data scientist training and placement in hyderabad

Data Science Course in Bangalore with Placement - 360DigiTMG said...

Truly mind blowing blog went amazed with the subject they have developed the content. These kind of posts really helpful to gain the knowledge of unknown things which surely triggers to motivate and learn the new innovative contents. Hope you deliver the similar successive contents forthcoming as well.

data science in bangalore

Post a Comment