ruby - 如何使用 Ruby 捕获屏幕?

标签 ruby winapi

我需要使用 Ruby 捕获屏幕,然后获取屏幕上每个像素的 RGB 像素值数组。

我尝试了 Windows API,并且可以对屏幕进行位转换并检索位图的句柄,但我不知道如何访问该句柄数据中的原始 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

最佳答案

我想出了如何做到这一点,所以我会为可能需要帮助的其他人发布解决方案。

GetDIBits Win32API 函数可用于访问存储在使用上述代码捕获的位图中的 RGB 数组。

http://msdn.microsoft.com/en-us/library/dd144879%28v=vs.85%29.aspx

关于ruby - 如何使用 Ruby 捕获屏幕?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8685068/

相关文章:

ruby - 将 PTY 输出与 Eventmachine 同步

ruby - 如何使用 Mechanize 单击链接来抓取订单的更多详细信息?

delphi - 如何在 win32pipe/console 上获取 "spy "?

c++ - 透明 TreeView 控件

ruby - 检查点并恢复 Ruby 中的堆

ruby - 如何将文件从 gem 复制到本地目录

c - Windows Hook 过程的问题

c++ - 在一个应用程序中创建两个窗口?

winapi - 如何以编程方式查找域 Controller /主域 Controller ?

ruby-on-rails - 如何使用 ActiveRecord 最好地处理每个模型的数据库连接?