class Win32::Screenshot::BitmapMaker

@private This is an internal class for taking the actual screenshots and not part of a public API.

Constants

DIB_RGB_COLORS
PW_RENDERFULLCONTENT
SM_CXVIRTUALSCREEN
SM_CYVIRTUALSCREEN
SM_XVIRTUALSCREEN
SM_YVIRTUALSCREEN
SRCCOPY

Public Class Methods

capture_screen(hwnd) click to toggle source
# File lib/win32/screenshot/bitmap_maker.rb, line 61
def capture_screen(hwnd)
  left, top, width, height = desktop.dimensions

  hScreenDC, hmemDC, hmemBM = prepare_object(hwnd, width, height)
  bit_blt(hmemDC, 0, 0, width, height, hScreenDC, left, top, SRCCOPY)
  create_bitmap(hScreenDC, hmemDC, hmemBM, width, height)
end
capture_window(hwnd) click to toggle source
# File lib/win32/screenshot/bitmap_maker.rb, line 53
def capture_window(hwnd)
  width, height = dimensions_for(hwnd)

  hScreenDC, hmemDC, hmemBM = prepare_object(hwnd, width, height)
  print_window(hwnd, hmemDC, PW_RENDERFULLCONTENT)
  create_bitmap(hScreenDC, hmemDC, hmemBM, width, height)
end
create_bitmap(hScreenDC, hmemDC, hmemBM, width, height) click to toggle source
# File lib/win32/screenshot/bitmap_maker.rb, line 77
def create_bitmap(hScreenDC, hmemDC, hmemBM, width, height)
  bitmap_size = width * height * 3 + width % 4 * height
  lpvpxldata = FFI::MemoryPointer.new(bitmap_size)

  # Bitmap header
  # http://www.fortunecity.com/skyscraper/windows/364/bmpffrmt.html
  bmInfo = [40, width, height, 1, 24, 0, 0, 0, 0, 0, 0, 0].pack('L3S2L6')
  di_bits(hmemDC, hmemBM, 0, height, lpvpxldata, bmInfo, DIB_RGB_COLORS)

  bmFileHeader = [
          19778,
          bitmap_size + 40 + 14,
          0,
          0,
          54
  ].pack('SLSSL')

  Image.new(bmFileHeader + bmInfo + lpvpxldata.read_string(bitmap_size), width, height)
ensure
  lpvpxldata.free
  delete_object(hmemBM)
  delete_dc(hmemDC)
  release_dc(0, hScreenDC)
end
desktop() click to toggle source
# File lib/win32/screenshot/bitmap_maker.rb, line 102
def desktop
  Win32::Screenshot::Desktop.new(
       get_system_metrics(SM_XVIRTUALSCREEN),
       get_system_metrics(SM_YVIRTUALSCREEN),
       get_system_metrics(SM_CXVIRTUALSCREEN),
       get_system_metrics(SM_CYVIRTUALSCREEN)
  )
end
dimensions_for(hwnd) click to toggle source
# File lib/win32/screenshot/bitmap_maker.rb, line 111
def dimensions_for(hwnd)
  rect = [0, 0, 0, 0].pack('l4')
  window_rect(hwnd.to_i, rect)
  left, top, width, height = rect.unpack('l4')

  [width - left, height - top]
end
prepare_object(hwnd, width, height) click to toggle source
# File lib/win32/screenshot/bitmap_maker.rb, line 69
def prepare_object(hwnd, width, height)
  hScreenDC = window_dc(hwnd)
  hmemDC = create_compatible_dc(hScreenDC)
  hmemBM = create_compatible_bitmap(hScreenDC, width, height)
  select_object(hmemDC, hmemBM)
  [hScreenDC, hmemDC, hmemBM]
end