# How to do a getPixel on Win32 using Ruby?

**URL:** <https://www.ruby-forum.com/t/how-to-do-a-getpixel-on-win32-using-ruby/178985>\
**Category:** Ruby\
**Created:** [November 14, 2009, 7:16pm UTC](https://www.ruby-forum.com/t/how-to-do-a-getpixel-on-win32-using-ruby/178985 "2009-11-14T19:16:35Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![winterheat](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7993a0/32.png) [@winterheat](https://www.ruby-forum.com/u/winterheat)\
**Post date:** [November 14, 2009, 7:16pm UTC](https://www.ruby-forum.com/t/how-to-do-a-getpixel-on-win32-using-ruby/178985/1 "2009-11-14T19:16:35Z")

</div>

Does someone know how to do a getPixel on Win32 using Ruby? (either to  
get a pixel’s color value on a window or on the whole screen). thanks.

---

<div class="post-metadata">

**Author:** ![winterheat](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7993a0/32.png) [@winterheat](https://www.ruby-forum.com/u/winterheat)\
**Post date:** [November 14, 2009, 7:20pm UTC](https://www.ruby-forum.com/t/how-to-do-a-getpixel-on-win32-using-ruby/178985/2 "2009-11-14T19:20:50Z")

</div>

Jian L. wrote:

> Does someone know how to do a getPixel on Win32 using Ruby? (either to  
> get a pixel’s color value on a window or on the whole screen). thanks.

ah… and also setPixel() too.

---

<div class="post-metadata">

**Author:** ![winterheat](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7993a0/32.png) [@winterheat](https://www.ruby-forum.com/u/winterheat)\
**Post date:** [November 14, 2009, 7:41pm UTC](https://www.ruby-forum.com/t/how-to-do-a-getpixel-on-win32-using-ruby/178985/3 "2009-11-14T19:41:44Z")

</div>

I thought it would be something like

user32 ‘GetPixel’, [‘L’, ‘I’, ‘I’], ‘L’

## as in

require ‘Win32API’

def user32(name, param\_types, return\_value)  
Win32API.new ‘user32’ , name, param\_types, return\_value  
end

set\_cursor\_pos = user32 ‘SetCursorPos’, [‘L’ , ‘L’], ‘I’  
mouse\_event = user32 ‘mouse\_event’, [‘L’ , ‘L’ , ‘L’ , ‘L’ , ‘L’], ‘V’

get\_pixel = user32 ‘GetPixel’, [‘L’, ‘I’, ‘I’], ‘L’

## get\_pixel.call 0, 0, 0

but there is an error giving out

get\_pixel.rb:4:in `initialize’: GetProcAddress: GetPixel or GetPixelA  
(RuntimeError)

```
    from get_pixel.rb:4:in `new'
    from get_pixel.rb:4:in `user32'
    from get_pixel.rb:10
```

---

<div class="post-metadata">

**Author:** ![winterheat](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7993a0/32.png) [@winterheat](https://www.ruby-forum.com/u/winterheat)\
**Post date:** [November 14, 2009, 7:43pm UTC](https://www.ruby-forum.com/t/how-to-do-a-getpixel-on-win32-using-ruby/178985/4 "2009-11-14T19:43:41Z")

</div>

Jian L. wrote:

> Jian L. wrote:
> 
> > Does someone know how to do a getPixel on Win32 using Ruby? (either to  
> > get a pixel’s color value on a window or on the whole screen). thanks.
> 
> ah… and also setPixel() too.

Do you want to draw onto the screen?

## If so, I think you should use a GUI toolkit, create a maximized window, without a title bar and border, and then draw onto it. For example, in wxRuby:

require “wx”

class MyApp \< Wx::App  
include Wx

def on\_init  
@mainwindow = Frame.new(nil, -1, “”, DEFAULT\_POSITION, DEFAULT\_SIZE,  
MAXIMIZE)  
@mainwindow.background\_colour = BLACK  
Timer.after(3000) do  
@mainwindow.paint do |dc|  
dc.pen = WHITE\_PEN  
dc.draw\_line(0, 0, 500, 500)  
end  
end  
Timer.after(5000){@mainwindow.close}  
@mainwindow.show  
end

end

## x = MyApp.new x.main\_loop

This creates a maximized, black window and after 3 seconds it draws a  
white line from (0|0) to (500|500). After five seconds (from the  
beginning) it closes the window.  
Tested with ruby 1.9.1p243 (2009-07-16 revision 24175) [i386-mingw32] on  
Windows Vista.

Marvin

---

<div class="post-metadata">

**Author:** ![winterheat](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7993a0/32.png) [@winterheat](https://www.ruby-forum.com/u/winterheat)\
**Post date:** [November 15, 2009, 2:10am UTC](https://www.ruby-forum.com/t/how-to-do-a-getpixel-on-win32-using-ruby/178985/5 "2009-11-15T02:10:21Z")

</div>

Marvin GÃ¼lker wrote:

> Do you want to draw onto the screen?
> 
> If so, I think you should use a GUI toolkit, create a maximized window,  
> without a title bar and border, and then draw onto it.

can wxRuby get a pixel’s color value off from the screen too? (not  
window but the screen)

---

<div class="post-metadata">

**Author:** ![winterheat](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7993a0/32.png) [@winterheat](https://www.ruby-forum.com/u/winterheat)\
**Post date:** [November 15, 2009, 12:59pm UTC](https://www.ruby-forum.com/t/how-to-do-a-getpixel-on-win32-using-ruby/178985/6 "2009-11-15T12:59:43Z")

</div>

Jian L. wrote:

> Marvin GÃ¼lker wrote:
> 
> > Do you want to draw onto the screen?
> > 
> > If so, I think you should use a GUI toolkit, create a maximized window,  
> > without a title bar and border, and then draw onto it.
> 
> can wxRuby get a pixel’s color value off from the screen too? (not  
> window but the screen)

## If you create the window like I showed, it will fill the _entire_ screen. There’s nothing else than the window to see (not even the taskbar!), so it’s equivalent if you get the pixel from your window or the screen. And so does it work:

require “wx”

class MyApp \< Wx::App  
include Wx

def on\_init  
@mainwindow = Frame.new(nil, -1, “”, DEFAULT\_POSITION, DEFAULT\_SIZE,  
MAXIMIZE)  
@mainwindow.background\_colour = BLACK  
Timer.after(3000) do  
@mainwindow.paint do |dc|  
dc.pen = WHITE\_PEN  
dc.draw\_line(0, 0, 500, 500)  
#Get the pixel color at (100|10) which should be black.  
col = Colour.new(255, 255, 255) #This means white  
dc.get\_pixel(100, 10, col)  
p col #=\> (0, 0, 0) #This means black  
end  
end  
Timer.after(5000){@mainwindow.close}  
@mainwindow.show  
end

end

## x = MyApp.new x.main\_loop

The #get\_pixel method seems to be directly imported from C, because it  
works like it would require a pointer. However, it works. The method to  
set a pixel directly, is #draw\_point.

Marvin
