SoFunction
Updated on 2024-11-14

Example of windows desktop screenshot code via python

This article introduces the realization of python windows desktop screenshot code examples, the text of the sample code through the introduction of the very detailed, for everyone to learn or work with certain reference to the learning value, you can refer to the next!

code example

import time

import win32api
import win32con
import win32gui
import win32ui


def get_desk():
  # Get the desktop
  hdesktop=()

  # Resolution adaptation
  width=(win32con.SM_CXVIRTUALSCREEN)
  height=(win32con.SM_CYVIRTUALSCREEN)
  left=(win32con.SM_XVIRTUALSCREEN)
  top=(win32con.SM_YVIRTUALSCREEN)

  # Create a device description table
  desktop_dc=(hdesktop)
  img_dc=(desktop_dc)

  # Create a memory device description table
  mem_dc=img_dc.CreateCompatibleDC()
  screenshot=()
  (img_dc,width,height)# Open up space for bitmap
  mem_dc.SelectObject(screenshot) # Save screenshot to Bitmap
  mem_dc.BitBlt((0,0),(width,height),img_dc,(left,top),) # Capture the image from the top left corner (0, 0) with a length and width of (w, h)

  # Save to file
  time_tup=(())
  format_time="%Y-%m-%d_%a_%H-%M-%S"
  cur_time=(format_time,time_tup)
  (mem_dc,'{}.bmp'.format(cur_time))

  # Free the memory
  mem_dc.DeleteDC()
  (())

if __name__ == '__main__':
  get_desk()

This is the whole content of this article.