Envision, Create, Share

Welcome to HBGames, a leading amateur game development forum and Discord server. All are welcome, and amongst our ranks you will find experts in their field from all aspects of video game design and development.

Screenshot Script

A simple and fast screenshot script from me:
Code:
########################

# ScreenCapture V1.0

# Autor Reborn

########################

 

 

class Bitmap

  CreateFile = Win32API.new('Kernel32.dll', 'CreateFile', 'piipiii', 'i')

  WriteFile = Win32API.new('Kernel32.dll', 'WriteFile', 'ipipp', 'i')

  CloseHandle = Win32API.new('Kernel32.dll', 'CloseHandle', 'i', 'i')

  RtlMoveMemory = Win32API.new('Kernel32.dll', 'RtlMoveMemory', 'pii', 'i')

  

  GENERIC_WRITE = 0x40000000

  OPEN_ALWAYS = 0x04

  

  def save(filename)

    towrite = ""

    hfBitmap = ["B"[0], "M"[0], width * height * 4, 0, 0, 14].pack('CCLSSL')

    infobmp  = [40, width, height, 1, 32, 0, 0, 0, 0, 0, 0, 0].pack('LLLSSLLLLLL')

    file = CreateFile.call(filename, GENERIC_WRITE, 0, 0, OPEN_ALWAYS, 0, 0)

    buff = "\0\0\0\0"

    WriteFile.call(file, hfBitmap + infobmp, hfBitmap.size() + infobmp.size(), buff, 0)

    WriteFile.call(file, address, width * height * 4, buff, 0)

    CloseHandle.call(file)

  end

  

  def address

    buffer = "xxxx"

    ad     = self.object_id * 2 + 16

    RtlMoveMemory.call(buffer, ad, 4); ad = buffer.unpack("L")[0] +  8

    RtlMoveMemory.call(buffer, ad, 4); ad = buffer.unpack("L")[0] + 16

    RtlMoveMemory.call(buffer, ad, 4); return buffer.unpack("L")[0]

  end

end

 

 

module Screen

  GetForegroundWindow = Win32API.new('User32.dll', 'GetForegroundWindow', '', 'i')

  GetPrivateProfileString = Win32API.new('Kernel32.dll', 'GetPrivateProfileString', 'ppppip', 'i')

  GetWindowDC = Win32API.new('User32.dll', 'GetWindowDC', 'i', 'i')

  ReleaseDC = Win32API.new('User32.dll', 'ReleaseDC', 'ii', 'i')

  CreateCompatibleBitmap = Win32API.new('Gdi32.dll', 'CreateCompatibleBitmap', 'iii', 'i')

  CreateCompatibleDC = Win32API.new('Gdi32.dll', 'CreateCompatibleDC', 'i', 'i')

  SelectObject = Win32API.new('Gdi32.dll', 'SelectObject', 'ii', 'i')

  BitBlt = Win32API.new('Gdi32.dll', 'BitBlt', 'iiiiiiiii', 'i')

  GetDIBits = Win32API.new('Gdi32.dll', 'GetDIBits', 'iiiippi', 'i')

  DeleteObject = Win32API.new('Gdi32.dll', 'DeleteObject', 'i', 'i')

  DeleteDC = Win32API.new('Gdi32.dll', 'DeleteDC', 'i', 'i')

  GetClientRect = Win32API.new('User32.dll', 'GetClientRect', 'ip', 'i')

  GetWindowRect = Win32API.new('User32.dll', 'GetWindowRect', 'ip', 'i')

  

  SRCCOPY        = 0xCC0020

  DIB_RGB_COLORS = 0x000000

  

  module_function

  

  def Shot(x=nil, y=nil, width=nil, height=nil)

    rect1 = Rect.new(0, 0, 1, 1)

    rect2 = getWindowRect()

    if x.nil? || y.nil? || width.nil? || height.nil?

      rect1 = getClientRect()

    else

      rect1 = Rect.new(x, y, width, height)

    end

    rect1.x   += (rect2.width - rect1.width) / 2

    rect1.y   += (rect2.height - rect1.height) - rect1.x

    bitmap    = Bitmap.new(rect1.width, rect1.height)

    infobmp   = [40, rect1.width, rect1.height, 1, 32, 0, 0, 0, 0, 0, 0, 0].pack('LLLSSLLLLLL')

    hdcScreen = GetWindowDC.call(hwnd())

    hbmScreen = CreateCompatibleBitmap.call(hdcScreen, rect1.width, rect1.height)

    hdcTemp   = CreateCompatibleDC.call(hdcScreen)

    hbmOld    = SelectObject.call(hdcTemp, hbmScreen)

    BitBlt.call(hdcTemp, 0, 0, rect1.width, rect1.height, hdcScreen, rect1.x, rect1.y, SRCCOPY);

    GetDIBits.call(hdcTemp, hbmScreen, 0, rect1.height, bitmap.address, infobmp, DIB_RGB_COLORS)

    SelectObject.call(hdcTemp, hbmOld);

    DeleteObject.call(hbmScreen);

    DeleteDC.call(hdcTemp);

    ReleaseDC.call(hwnd, hdcScreen)

    return bitmap

  end

          

  def getWindowRect()

    buff = "\0" * 16

    GetWindowRect.call(hwnd, buff)

    buff = buff.unpack('llll')

    return Rect.new(0, 0, buff[2] - buff[0], buff[3] - buff[1])

  end

          

  def getClientRect()

    buff = "\0" * 16

    GetClientRect.call(hwnd, buff)

    buff = buff.unpack('llll')

    return Rect.new(buff[0], buff[1], buff[2], buff[3])

  end

  

  def hwnd()

    return GetForegroundWindow.call()

  end

end

 
 

Thank you for viewing

HBGames is a leading amateur video game development forum and Discord server open to all ability levels. Feel free to have a nosey around!

Discord

Join our growing and active Discord server to discuss all aspects of game making in a relaxed environment. Join Us

Content

  • Our Games
  • Games in Development
  • Emoji by Twemoji.
    Top