在ruby中使用GetDIBits函数时遇到麻烦

我有下面的Ruby代码,我正在使用Windows API调用,我应该可以使用函数GetDIBits从我创build的位图检索一个RGB值的数组,用于AI处理。 我需要给GetDIBits函数一个BITMAPINFO结构以及一些其他variables。 我应该可以用其他variables,但是如何创build一个ruby结构(使用Windows API),可以用于这个function?

请看下面的代码

如果您可以完成GetDIBits函数,以便可以检索RGB值数组,我将不胜感激。

感谢Martin

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 Win32API.new("Gdi32.dll","GetDIBits",["L","L","L","L","L","L","P"],"L").call(hcdc,hbitmap,1,1200,0,"DIB_RGB_COLORS") pixelarray = Array.new #Need a .call to fill the pixelarray array 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 

这个代码似乎得到了一些数据,虽然我没有仔细看过,看是否是来自屏幕的颜色:

 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 

当我在Windows中运行ruby 1.9.2p180时的输出是:

 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 

如果您对我在这里做什么有任何疑问,请告诉我。