Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to capture a part of a screen using Ruby on Windows?

Instead of using some third party app, I'd like to write an app in Ruby that when invoked, will capture the full screen and save it in c:\screenshot\snap000001.png

The graphic package is readily there, but how can you capture a region from the full screen so as to save it?

This program is to be invoked by some hot-key, such as setting it to be running when CTRL-PrtScn is pressed, or CTRL-CTRL (both control on left and right), or ALT-ALT.

like image 225
nonopolarity Avatar asked Feb 28 '23 02:02

nonopolarity


1 Answers

I haven't tried it (I'm not on windows). But you could use Win32::Screenshot.

While looking around, I've found the following, which does the screenshot using that library

width, height, bitmap = Win32::Screenshot.desktop
img_lst = ImageList.new
img_lst.from_blob(bitmap)
img_lst.write('public/screen.png')

And should write your screenshot as a png file.

like image 122
Damien MATHIEU Avatar answered Mar 11 '23 06:03

Damien MATHIEU