Aug 29

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!

3 Responses to “Take screenshot with Ruby on Rails”

  1. Andres says:

    A couple of fixes:
    require ‘rubygems’
    require ‘win32screenshot’
    require ‘RMagick’
    include Magick

    width, height, bmp = Win32::Screenshot.foreground
    img = Magick::Image.from_blob(bmp)[0]
    img.write(”./screenshot.png”)

  2. Andres says:

    btw..you misspelled blob with blog in your post, so you’re calling a funciont that doesn’t exist:
    from_blob. My “couple of fixes” works correctly.

  3. william says:

    My bad. Thanks.

Leave a Reply

Security Code: