簡體   English   中英

如何使用Ruby捕獲屏幕?

[英]How do I capture the screen using Ruby?

我需要使用Ruby捕獲屏幕,然后為屏幕上的每個像素獲取RGB像素值數組。

我嘗試使用Windows API,並且可以對屏幕進行位控制並檢索位圖的句柄,但是我不知道如何訪問該句柄數據中的原始RGB值。

這就是我目前所擁有的,它足夠快,但是我需要將hbitmap中的RGB值放入一個可以使用的數組中。

與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/zh-cn/library/dd144879%28v=vs.85%29.aspx

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM