简体   繁体   中英

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.

您可以使用xwd(1)截取根窗口的屏幕截图:

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

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 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM