# Making screenshots with Ruby

**URL:** <https://www.ruby-forum.com/t/making-screenshots-with-ruby/51116>\
**Category:** Ruby\
**Created:** [December 26, 2005, 8:13pm UTC](https://www.ruby-forum.com/t/making-screenshots-with-ruby/51116 "2005-12-26T20:13:37Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![thomas](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/e36b37/32.png) [@thomas](https://www.ruby-forum.com/u/thomas)\
**Post date:** [December 26, 2005, 8:13pm UTC](https://www.ruby-forum.com/t/making-screenshots-with-ruby/51116/1 "2005-12-26T20:13:37Z")

</div>

Hello,

Can anyone help me with this one? Been looking for information about it,  
but couldn’t find any…

I want my program to make a screenshot of the screen (or better: a  
specific window), and it should be able to get some data from the  
screenshot (compare parts to other images)

Does anyone know how to tackle this problem?

Thanks,

Thomas

---

<div class="post-metadata">

**Author:** ![thomas](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/e36b37/32.png) [@thomas](https://www.ruby-forum.com/u/thomas)\
**Post date:** [December 26, 2005, 8:35pm UTC](https://www.ruby-forum.com/t/making-screenshots-with-ruby/51116/2 "2005-12-26T20:35:20Z")

</div>

Thomas wrote:

> Hello,
> 
> Can anyone help me with this one? Been looking for information about it,  
> but couldn’t find any…
> 
> I want my program to make a screenshot of the screen (or better: a  
> specific window), and it should be able to get some data from the  
> screenshot (compare parts to other images)
> 
> Does anyone know how to tackle this problem?

Using the excellent RMagick:  
[http://redux.imagemagick.org/RMagick/doc/image1.html#capture](http://redux.imagemagick.org/RMagick/doc/image1.html#capture)

Cheers,

---

<div class="post-metadata">

**Author:** ![thomas](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/e36b37/32.png) [@thomas](https://www.ruby-forum.com/u/thomas)\
**Post date:** [December 27, 2005, 7:10am UTC](https://www.ruby-forum.com/t/making-screenshots-with-ruby/51116/3 "2005-12-27T07:10:16Z")

</div>

> I want my program to make a screenshot of the screen

I think the rubycocoa example simpleapp does that, you could look at  
how they do it.

---

<div class="post-metadata">

**Author:** ![thomas](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/e36b37/32.png) [@thomas](https://www.ruby-forum.com/u/thomas)\
**Post date:** [December 28, 2005, 11:52pm UTC](https://www.ruby-forum.com/t/making-screenshots-with-ruby/51116/4 "2005-12-28T23:52:32Z")

</div>

Unfortunately cacao is for mac, and the RMagick function is for x  
(linux), while I need Windows… Any tips? Maybe there’s a way using the  
Win32 API?

I don’t know how to create a “DestDC” for the bitblt function  
([Technical documentation | Microsoft Learn](http://msdn.microsoft.com/library/default.asp?url=/library/en-us/gdi/bitmaps_0fzo.asp)),  
so I have no idea what to do… Currently I’m using a Visual Basic  
program which takes a screenshot (using the clipboard, automatically  
pressing the printscreen button) but ofcourse that’s not a good  
solution, and it needs harddrive access…

Thanks.

---

<div class="post-metadata">

**Author:** ![thomas](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/e36b37/32.png) [@thomas](https://www.ruby-forum.com/u/thomas)\
**Post date:** [December 29, 2005, 12:01am UTC](https://www.ruby-forum.com/t/making-screenshots-with-ruby/51116/5 "2005-12-29T00:01:45Z")

</div>

Oh and forgot to add - I’m using them later with RMagick, so I’d want  
them loaded in memory in a way that RMagick can understand them, but if  
it has to it can be saved to file too 🙂

---

<div class="post-metadata">

**Author:** ![thomas](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/e36b37/32.png) [@thomas](https://www.ruby-forum.com/u/thomas)\
**Post date:** [December 31, 2005, 12:27pm UTC](https://www.ruby-forum.com/t/making-screenshots-with-ruby/51116/6 "2005-12-31T12:27:17Z")

</div>

I would be interestde in how you solved this

Regards,  
Bryan Webb

---

<div class="post-metadata">

**Author:** ![thomas](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/e36b37/32.png) [@thomas](https://www.ruby-forum.com/u/thomas)\
**Post date:** [December 31, 2005, 12:48pm UTC](https://www.ruby-forum.com/t/making-screenshots-with-ruby/51116/7 "2005-12-31T12:48:31Z")

</div>

removed\_email\_address@domain.invalid wrote:

> Oh and forgot to add - I’m using them later with RMagick, so I’d want  
> them loaded in memory in a way that RMagick can understand them, but if  
> it has to it can be saved to file too 🙂

There are a couple of ways to construct an RMagick image without reading  
an image file. If the screenshot data is in a Ruby string in one of the  
formats that ImageMagick/GraphicsMagick understands (JPEG, GIF, BMP, or  
one of the 80+ others) then you can use Image.from\_blob[1].

If the screenshot data is in a Ruby string (or an instance of any class  
with a to\_str method, such as NArray) and is simply “raw” pixel data in  
scanline order (left-to-right, top-to-bottom) you use the  
Image.import\_pixels method. It’s blindingly fast. (I’d give you a URL to  
the doc, but the online doc is sufficiently out-of-date that it doesn’t  
describe how import\_pixels exploits to\_str. You can get the details from  
your local copy of the RMagick doc.)

[1] [RMagick 1.15.0: class Image (class and instance methods, part 1)](http://www.simplesystems.org/RMagick/doc/image1.html#from_blob)
