Take screenshot with Ruby on Rails

How can our code take a screenshot of a webpage, and save it to an image file ?

It turns out it’s easy. First, we need to

gem install win32screenshot

Then, write code something like this:

require ‘win32screenshot’
width, height, bmp = Win32::Screenshot.foreground
img = Magick::Image.from_blog(bmp)[0]
img.write(RAILS_ROOT + “/public/images/screenshot.png”)

Voila, and you get the screenshot!

Leave a Comment

Please note: Comment moderation is enabled and may delay your comment. There is no need to resubmit your comment.