English 中文(简体)
Programmatically take ScreenShot of Desktop in Ruby?
原标题:

I asked this question about taking a picture of a webpage programmatically, and I ve downloaded and got webkit2png working (taking pictures of HTML pages like blogs and whatnot). So cool, thanks for showing me that!

Now I would like to start doing more, like being able to take pictures of Flash websites after they have loaded, and of my desktop.

Is it possible to take pictures of Flash websites with webkit2png (considering you might have to wait a few seconds for it to load)?

But the main question is, how do I programmatically take pictures of the desktop? That would allow me to have much more control over what s going on.

最佳回答

This is an awesome question!

A couple of years ago I has to work on a similar project. I found a library, called watir, you can use to control system browsers from Ruby. At the time I checked, it wasn t really reliable in a Linux environment, but right now it seems to be pretty solid.

Here s a couple of links:

I have never tried this solution so I would be really happy if you can write here a feedback if you decide to go with Watir. All the examples targets a Windows server, I didn t found a valid tutorial using a Linux + Firefox environment.

问题回答

You can use xwd(1) to take a screenshot of the root window:

xwd -display :0 -root|xwdtopnm |pnmtopng > $1

You can use Watir WebDriver like this:

$ irb
irb(main):001:0> require  watir-webdriver 
=> true
irb(main):002:0> browser = Watir::Browser.new
=> #<Watir::Browser:0x136da92fad77d562 url="about:blank" title="">
irb(main):003:0> browser.goto  http://stackoverflow.com/questions/1733715/programmatically-take-screenshot-of-desktop-in-ruby 
=> "http://stackoverflow.com/questions/1733715/programmatically-take-screenshot-of-desktop-in-ruby"
irb(main):004:0> browser.screenshot.save  screenshot.png 
=> #<File:screenshot.png (closed)>
irb(main):005:0>

which seems to do the job rather well! I tested this on my Mac, but I d be surprised if it didn t work well on Linux too.





相关问题
Capture desktop without my window

I want to create a recording of desktop content, but without a certain window. The role of this window is to provide a text that will be read, while recording desktop changes. Any suggestions?

Logout from an desktop application to change user in C#.net

I have designed an desktop application using C#.net that has many users. Each USer has specific rights. The User logs into the system when the application first starts and the UserID number is stored ...

Programmatically take ScreenShot of Desktop in Ruby?

I asked this question about taking a picture of a webpage programmatically, and I ve downloaded and got webkit2png working (taking pictures of HTML pages like blogs and whatnot). So cool, thanks for ...

热门标签