# Screen capture to display it or to save it to a file possible?

**URL:** <https://www.ruby-forum.com/t/screen-capture-to-display-it-or-to-save-it-to-a-file-possible/149711>\
**Category:** Ruby\
**Created:** [September 28, 2008, 8:30pm UTC](https://www.ruby-forum.com/t/screen-capture-to-display-it-or-to-save-it-to-a-file-possible/149711 "2008-09-28T20:30:49Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![liketofindoutwhy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/e47c2d/32.png) [@liketofindoutwhy](https://www.ruby-forum.com/u/liketofindoutwhy)\
**Post date:** [September 28, 2008, 8:30pm UTC](https://www.ruby-forum.com/t/screen-capture-to-display-it-or-to-save-it-to-a-file-possible/149711/1 "2008-09-28T20:30:49Z")

</div>

i would like do screen capture and then to display it in that app’s  
window, or be able to save the file in hard disk as a GIF or PNG. I  
wonder if TCL Ruby or RMagick can already do that? Thanks.

---

<div class="post-metadata">

**Author:** ![liketofindoutwhy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/e47c2d/32.png) [@liketofindoutwhy](https://www.ruby-forum.com/u/liketofindoutwhy)\
**Post date:** [September 28, 2008, 8:36pm UTC](https://www.ruby-forum.com/t/screen-capture-to-display-it-or-to-save-it-to-a-file-possible/149711/2 "2008-09-28T20:36:05Z")

</div>

On Sep 28, 11:29 am, liketofindoutwhy [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> i would like do screen capture and then to display it in that app’s  
> window, or be able to save the file in hard disk as a GIF or PNG. I  
> wonder if TCL Ruby or RMagick can already do that? Thanks.

oh i actually mean Ruby/Tk

---

<div class="post-metadata">

**Author:** ![liketofindoutwhy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/e47c2d/32.png) [@liketofindoutwhy](https://www.ruby-forum.com/u/liketofindoutwhy)\
**Post date:** [September 28, 2008, 10:48pm UTC](https://www.ruby-forum.com/t/screen-capture-to-display-it-or-to-save-it-to-a-file-possible/149711/3 "2008-09-28T22:48:07Z")

</div>

liketofindoutwhy wrote:

> On Sep 28, 11:29 am, liketofindoutwhy [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
> wrote:
> 
> > i would like do screen capture and then to display it in that app’s  
> > window, or be able to save the file in hard disk as a GIF or PNG. I  
> > wonder if TCL Ruby or RMagick can already do that? Thanks.
> 
> oh i actually mean Ruby/Tk

RMagick can do it on systems that have X Window. In other words, not MS  
Windows. [http://studio.imagemagick.org/RMagick/doc/image1.html#capture](http://studio.imagemagick.org/RMagick/doc/image1.html#capture).

---

<div class="post-metadata">

**Author:** ![liketofindoutwhy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/e47c2d/32.png) [@liketofindoutwhy](https://www.ruby-forum.com/u/liketofindoutwhy)\
**Post date:** [September 30, 2008, 3:05am UTC](https://www.ruby-forum.com/t/screen-capture-to-display-it-or-to-save-it-to-a-file-possible/149711/4 "2008-09-30T03:05:56Z")

</div>

On Sep 28, 1:46 pm, Tim H. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Windows.[http://studio.imagemagick.org/RMagick/doc/image1.html#capture](http://studio.imagemagick.org/RMagick/doc/image1.html#capture).  
> hm… so can you install x-window capabilities on XP or Vista… will  
> that work? I think the Cygwin install x-window capabilities on  
> Windows.

---

<div class="post-metadata">

**Author:** ![liketofindoutwhy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/e47c2d/32.png) [@liketofindoutwhy](https://www.ruby-forum.com/u/liketofindoutwhy)\
**Post date:** [September 30, 2008, 7:45am UTC](https://www.ruby-forum.com/t/screen-capture-to-display-it-or-to-save-it-to-a-file-possible/149711/5 "2008-09-30T07:45:03Z")

</div>

2008/9/30 liketofindoutwhy [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> > RMagick can do it on systems that have X Window. In other words, not MS  
> > Windows.[http://studio.imagemagick.org/RMagick/doc/image1.html#capture](http://studio.imagemagick.org/RMagick/doc/image1.html#capture).
> 
> hm… so can you install x-window capabilities on XP or Vista… will  
> that work? I think the Cygwin install x-window capabilities on  
> Windows.

There is screen\_capture method in watir. But it is unstable.  
Here is the revised screen\_capture method for Windows:

require ‘win32ole’  
require ‘Win32API’

def screen\_capture(filename, active\_window\_only=false)

keybd\_event = Win32API.new(“user32”, “keybd\_event”, [‘I’,‘I’,‘L’,‘L’],  
‘V’)  
openClipboard = Win32API.new(‘user32’, ‘OpenClipboard’, [‘L’], ‘I’)  
setClipboardData = Win32API.new(‘user32’, ‘SetClipboardData’, [‘I’,  
‘I’], ‘I’)  
closeClipboard = Win32API.new(‘user32’, ‘CloseClipboard’, [], ‘I’)  
globalAlloc = Win32API.new(‘kernel32’, ‘GlobalAlloc’, [‘I’, ‘I’], ‘I’)  
globalLock = Win32API.new(‘kernel32’, ‘GlobalLock’, [‘I’], ‘I’)  
globalUnlock = Win32API.new(‘kernel32’, ‘GlobalUnlock’, [‘I’], ‘I’)  
memcpy = Win32API.new(‘msvcrt’, ‘memcpy’, [‘I’, ‘P’, ‘I’], ‘I’)

wsh = WIN32OLE.new(‘Wscript.Shell’)

if not active\_window\_only  
keybd\_event.Call(0x2C,0,0,0) # Print Screen  
else  
keybd\_event.Call(0x2C,1,0,0) # Alt+Print Screen  
end

exec = wsh.Exec(‘mspaint.exe’)  
wsh.AppActivate(exec.ProcessID)  
sleep(1)

# Ctrl + V : Paste

wsh.SendKeys(“^v”)  
sleep(1)

# Alt F + A : Save As

wsh.SendKeys(“%fa”)

# copy filename to clipboard

hmem = globalAlloc.Call(0x0002, filename.length+1)  
mem = globalLock.Call(hmem)  
memcpy.Call(mem, filename, filename.length+1)  
globalUnlock.Call(hmem)  
openClipboard.Call(0)  
setClipboardData.Call(1, hmem)  
closeClipboard.Call  
sleep(1)

# Ctrl + V : Paste

wsh.SendKeys(“^v”)

if filename[/.gif/i]  
wsh.SendKeys(“{TAB}g”)  
elsif filename[/.jpg/i]  
wsh.SendKeys(“{TAB}j”)  
end  
wsh.SendKeys(“~”) # enter  
sleep(1)  
wsh.SendKeys(“yy”)  
sleep(4)  
exec.Terminate  
end

screen\_capture(“c:\test.gif”)

Regards,

Park H.
