【发布时间】:2012-01-01 05:25:35
【问题描述】:
我需要使用 Ruby 捕获屏幕,然后为屏幕上的每个像素获取一个 RGB 像素值数组。
我尝试了 Windows API,并且可以对屏幕进行 bitblt 并检索位图的句柄,但我不知道如何访问此句柄数据中的原始 RGB 值。
这是我目前所拥有的,它足够快,但我需要将 RGB 值从 hbitmap 获取到我可以使用的数组中。
任何与 Bitblt 一样快但更简单的东西也会受到赞赏。
def getscreen()
width = Win32API.new("User32.dll","GetSystemMetrics",["L"],"L").call(0)
height = Win32API.new("User32.dll","GetSystemMetrics",["L"],"L").call(1)
#Get desktop DC, create a compatible dc, create a comaptible bitmap and select into compatible dc.
hddc = Win32API.new("User32.dll","GetDC",["L"],"L").call(Win32API.new("User32.dll","GetDesktopWindow",[],"L").call)
hcdc = Win32API.new("Gdi32.dll","CreateCompatibleDC",["L"],"L").call(hddc)
hbitmap = Win32API.new("Gdi32.dll","CreateCompatibleBitmap",["L","L","L"],"L").call(hddc,width,height)
Win32API.new("Gdi32.dll","SelectObject",["L","L"],"L").call(hcdc,hbitmap)
Win32API.new("Gdi32.dll","BitBlt",["L","L","L","L","L","L","L","L","P"],"L").call(hcdc,0,0,width,height,hddc,0,0,"SRCCOPY|CAPTUREBLT")
#save hbitmap to stream of byte as you mentioned
puts hbitmap
#
Win32API.new("User32.dll","ReleaseDC",["L","L"],"L").call(Win32API.new("User32.dll","GetDesktopWindow",[],"L").call,hddc)
Win32API.new("Gdi32.dll","DeleteDC",["L"],"L").call(hcdc)
Win32API.new("Gdi32.dll","DeleteObject",["L"],"L").call(hbitmap)
#Print screen width and height
puts "Screen width: #{width}"
puts "Screen height: #{height}"
end
【问题讨论】:
-
嘿,不知道 ruby 附带了 win32API。不完全是一个解决方案,但这可能会有所帮助:ruby-forum.com/topic/166943
-
我已经尝试了上面的 stackflow 链接,在 1.9.3 上无法让它为我编译
-
是的,我查看了上面的第一个链接,它似乎有点复杂,但我可以通过 ruby 或通过 windows API(通过 Win32Api.new)完成一些更简单的事情
-
我再次查看了 stackover flow 链接,我认为我无法从中获取 rgb 值