Skip to content

Instantly share code, notes, and snippets.

@ismailakkila
Created November 14, 2017 08:54
Show Gist options
  • Save ismailakkila/39e534756a6670125acb119a8b2d1981 to your computer and use it in GitHub Desktop.
Save ismailakkila/39e534756a6670125acb119a8b2d1981 to your computer and use it in GitHub Desktop.
ch8_screenshotter.py
#ch8_screenshotter.py
import win32api
import win32ui
import win32gui
import win32con
filepath = ""
width = win32api.GetSystemMetrics(win32con.SM_CXVIRTUALSCREEN)
height = win32api.GetSystemMetrics(win32con.SM_CYVIRTUALSCREEN)
left = win32api.GetSystemMetrics(win32con.SM_XVIRTUALSCREEN)
top = win32api.GetSystemMetrics(win32con.SM_YVIRTUALSCREEN)
handle_desktop = win32gui.GetDesktopWindow()
dc_desktop = win32gui.GetWindowDC(handle_desktop)
dc_img = win32ui.CreateDCFromHandle(dc_desktop)
dc_mem = dc_img.CreateCompatibleDC()
screenshot = win32ui.CreateBitmap()
screenshot.CreateCompatibleBitmap(dc_img, width, height)
dc_mem.SelectObject(screenshot)
dc_mem.BitBlt((0, 0), (width, height), dc_img, (left, top), win32con.SRCCOPY)
screenshot.SaveBitmapFile(dc_mem , filepath)
dc_mem.DeleteDC()
win32gui.DeleteObject(screenshot.GetHandle())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment