Этот код, кажется, получает некоторые данные, хотя я недостаточно внимательно изучил, чтобы определить, являются ли это цветами с экрана:
require 'Win32API'
module Foo
GetSystemMetrics = Win32API.new "User32.dll", "GetSystemMetrics", ["L"], "L"
GetDC = Win32API.new "User32.dll", "GetDC", ["L"], "L"
GetDesktopWindow = Win32API.new "User32.dll", "GetDesktopWindow", [], "L"
CreateCompatibleDC = Win32API.new "Gdi32.dll", "CreateCompatibleDC", ["L"], "L"
CreateCompatibleBitmap = Win32API.new "Gdi32.dll", "CreateCompatibleBitmap", ["L","L","L"], "L"
SelectObject = Win32API.new "Gdi32.dll","SelectObject", ["L","L"], "L"
BitBlt = Win32API.new "Gdi32.dll","BitBlt", ["L","L","L","L","L","L","L","L","L"], "L"
CAPTUREBLT = 0x40000000
SRCCOPY = 0x00CC0020
GetDIBits = Win32API.new "Gdi32.dll","GetDIBits", ["L","L","L","L","P","P","L"], "L"
DIB_RGB_COLORS = 0
BI_RGB = 0
def self.getscreen
width = GetSystemMetrics.call 0
height = GetSystemMetrics.call 1
puts "Screen width: #{width}"
puts "Screen height: #{height}"
desktop_handle = GetDesktopWindow.call
raise "GetDesktopWindow failed" if desktop_handle == 0
hddc = GetDC.call desktop_handle
raise "Get DC failed." if hddc == 0
hcdc = CreateCompatibleDC.call hddc
raise "CreateCompatibleDC failed" if hcdc == 0
hbitmap = CreateCompatibleBitmap.call hddc, width, height
raise "CreateCompatibleBitmap failed" if hbitmap == 0
result = SelectObject.call hcdc, hbitmap
raise "SelectObject failed" if result == 0 # not sure if this is right
result = BitBlt.call hcdc, 0, 0, width, height, hddc, 0, 0, SRCCOPY | CAPTUREBLT
raise "BitBlt failed" if result == 0
SelectObject.call hcdc, 0 # attempt to deselect the bitmap because GetDIBits requires it?
size_in_bytes = width*height*4
bitmap_info = [40, #biSize
width,
height,
1, #biPlanes
32, #biBitCount
BI_RGB, #biCompression
size_in_bytes,
2000, #biXPelsPerMeter: not used?
2000, #biYPelsPerMeter: not used?
0, # biClrUsed
0 # biClrImportant
].pack("LLLSSLLLLLL") #+ [0,0,0,0].pack("LLLL")
buffer = (" " * size_in_bytes).force_encoding("BINARY")
result = GetDIBits.call hcdc, hbitmap, 0, height, buffer, bitmap_info, DIB_RGB_COLORS
raise "GetDIBits failed." if result == 0
puts "Number of scan lines copied: #{result}"
puts "buffer contents: " + buffer.strip.inspect[0,100] + " ..."
puts "First pixel = %02x,%02x,%02x,%02x" % [buffer[0].ord, buffer[1].ord, buffer[2].ord, buffer[3].ord]
return buffer
end
end
Foo.getscreen
Вывод, когда я запускаю его с ruby 1.9.2p180 в Windows:
Screen width: 1366
Screen height: 768
Number of scan lines copied: 768
buffer contents: "\x0F\f\a\xFF\x0F\f\a\xFF\x0F\f\a\xFF\x0F\f\a\xFF\x0F\f\a\xFF\x
0F\f\a\xFF\x0F\f\a\xFF\x0F\f\a\xFF\x0 ...
First pixel = 0f,0c,07,ff
Дайте мне знать, если у вас есть вопросы о том, что я здесь делаю.