Screenshot Script

A simple and fast screenshot script from me:
  1. ########################
  2. # ScreenCapture V1.0
  3. # Autor Reborn
  4. ########################
  5.  
  6.  
  7. class Bitmap
  8.   CreateFile = Win32API.new('Kernel32.dll', 'CreateFile', 'piipiii', 'i')
  9.   WriteFile = Win32API.new('Kernel32.dll', 'WriteFile', 'ipipp', 'i')
  10.   CloseHandle = Win32API.new('Kernel32.dll', 'CloseHandle', 'i', 'i')
  11.   RtlMoveMemory = Win32API.new('Kernel32.dll', 'RtlMoveMemory', 'pii', 'i')
  12.  
  13.   GENERIC_WRITE = 0x40000000
  14.   OPEN_ALWAYS = 0x04
  15.  
  16.   def save(filename)
  17.     towrite = ""
  18.     hfBitmap = ["B"[0], "M"[0], width * height * 4, 0, 0, 14].pack('CCLSSL')
  19.     infobmp  = [40, width, height, 1, 32, 0, 0, 0, 0, 0, 0, 0].pack('LLLSSLLLLLL')
  20.     file = CreateFile.call(filename, GENERIC_WRITE, 0, 0, OPEN_ALWAYS, 0, 0)
  21.     buff = "\0\0\0\0"
  22.     WriteFile.call(file, hfBitmap + infobmp, hfBitmap.size() + infobmp.size(), buff, 0)
  23.     WriteFile.call(file, address, width * height * 4, buff, 0)
  24.     CloseHandle.call(file)
  25.   end
  26.  
  27.   def address
  28.     buffer = "xxxx"
  29.     ad     = self.object_id * 2 + 16
  30.     RtlMoveMemory.call(buffer, ad, 4); ad = buffer.unpack("L")[0] +  8
  31.     RtlMoveMemory.call(buffer, ad, 4); ad = buffer.unpack("L")[0] + 16
  32.     RtlMoveMemory.call(buffer, ad, 4); return buffer.unpack("L")[0]
  33.   end
  34. end
  35.  
  36.  
  37. module Screen
  38.   GetForegroundWindow = Win32API.new('User32.dll', 'GetForegroundWindow', '', 'i')
  39.   GetPrivateProfileString = Win32API.new('Kernel32.dll', 'GetPrivateProfileString', 'ppppip', 'i')
  40.   GetWindowDC = Win32API.new('User32.dll', 'GetWindowDC', 'i', 'i')
  41.   ReleaseDC = Win32API.new('User32.dll', 'ReleaseDC', 'ii', 'i')
  42.   CreateCompatibleBitmap = Win32API.new('Gdi32.dll', 'CreateCompatibleBitmap', 'iii', 'i')
  43.   CreateCompatibleDC = Win32API.new('Gdi32.dll', 'CreateCompatibleDC', 'i', 'i')
  44.   SelectObject = Win32API.new('Gdi32.dll', 'SelectObject', 'ii', 'i')
  45.   BitBlt = Win32API.new('Gdi32.dll', 'BitBlt', 'iiiiiiiii', 'i')
  46.   GetDIBits = Win32API.new('Gdi32.dll', 'GetDIBits', 'iiiippi', 'i')
  47.   DeleteObject = Win32API.new('Gdi32.dll', 'DeleteObject', 'i', 'i')
  48.   DeleteDC = Win32API.new('Gdi32.dll', 'DeleteDC', 'i', 'i')
  49.   GetClientRect = Win32API.new('User32.dll', 'GetClientRect', 'ip', 'i')
  50.   GetWindowRect = Win32API.new('User32.dll', 'GetWindowRect', 'ip', 'i')
  51.  
  52.   SRCCOPY        = 0xCC0020
  53.   DIB_RGB_COLORS = 0x000000
  54.  
  55.   module_function
  56.  
  57.   def Shot(x=nil, y=nil, width=nil, height=nil)
  58.     rect1 = Rect.new(0, 0, 1, 1)
  59.     rect2 = getWindowRect()
  60.     if x.nil? || y.nil? || width.nil? || height.nil?
  61.       rect1 = getClientRect()
  62.     else
  63.       rect1 = Rect.new(x, y, width, height)
  64.     end
  65.     rect1.x   += (rect2.width - rect1.width) / 2
  66.     rect1.y   += (rect2.height - rect1.height) - rect1.x
  67.     bitmap    = Bitmap.new(rect1.width, rect1.height)
  68.     infobmp   = [40, rect1.width, rect1.height, 1, 32, 0, 0, 0, 0, 0, 0, 0].pack('LLLSSLLLLLL')
  69.     hdcScreen = GetWindowDC.call(hwnd())
  70.     hbmScreen = CreateCompatibleBitmap.call(hdcScreen, rect1.width, rect1.height)
  71.     hdcTemp   = CreateCompatibleDC.call(hdcScreen)
  72.     hbmOld    = SelectObject.call(hdcTemp, hbmScreen)
  73.     BitBlt.call(hdcTemp, 0, 0, rect1.width, rect1.height, hdcScreen, rect1.x, rect1.y, SRCCOPY);
  74.     GetDIBits.call(hdcTemp, hbmScreen, 0, rect1.height, bitmap.address, infobmp, DIB_RGB_COLORS)
  75.     SelectObject.call(hdcTemp, hbmOld);
  76.     DeleteObject.call(hbmScreen);
  77.     DeleteDC.call(hdcTemp);
  78.     ReleaseDC.call(hwnd, hdcScreen)
  79.     return bitmap
  80.   end
  81.          
  82.   def getWindowRect()
  83.     buff = "\0" * 16
  84.     GetWindowRect.call(hwnd, buff)
  85.     buff = buff.unpack('llll')
  86.     return Rect.new(0, 0, buff[2] - buff[0], buff[3] - buff[1])
  87.   end
  88.          
  89.   def getClientRect()
  90.     buff = "\0" * 16
  91.     GetClientRect.call(hwnd, buff)
  92.     buff = buff.unpack('llll')
  93.     return Rect.new(buff[0], buff[1], buff[2], buff[3])
  94.   end
  95.  
  96.   def hwnd()
  97.     return GetForegroundWindow.call()
  98.   end
  99. end
  100.  
Expand to see the code.
Neat. From the looks of it, this should work in XP, VX, and VX Ace, right? Do you do a lot of Windows development?
rey meustrus wrote:
Do you do a lot of Windows development?

No, i'm just good at using google 😀

Original thread: https://www.hbgames.org/forums/viewtopic.php?t=78587