hwnd = 0 # 窗口的编号,0号表示当前活跃窗口 # 根据窗口句柄获取窗口的设备上下文DC(Divice Context) hwndDC = win32gui.GetWindowDC(hwnd) # 根据窗口的DC获取mfcDC mfcDC = win32ui.CreateDCFromHandle(hwndDC) # mfcDC创建可兼容的DC saveDC = mfcDC.CreateCompatibleDC() # 创建bigmap准备保存图片 saveBitMap = win32ui.CreateBitmap() # 获取监控器信息 MoniterDev = win32api.EnumDisplayMonitors(None, None) w = MoniterDev[0][2][2] h = MoniterDev[0][2][3] # print w,h   #图片大小 # 为bitmap开辟空间 saveBitMap.CreateCompatibleBitmap(mfcDC, x2 - x1, y2 - y1) # 高度saveDC,将截图保存到saveBitmap中 saveDC.SelectObject(saveBitMap) # 截取从(x1, y1)长宽为(x2 - x1, y2 - y1)的图片 saveDC.BitBlt((0, 0), (x2 - x1, y2 - y1), mfcDC, (x1, y1), win32con.SRCCOPY) if not os.path.exists(os.path.dirname(os.path.abspath(filename))): os.makedirs(os.path.dirname(os.path.abspath(filename))) saveBitMap.SaveBitmapFile(saveDC, filename) except Exception as e: print('截图失败,失败原因:{}'.format(e)) def debug(): printscreen(50, 10, 500, 500, 'tmp/tmp.png') if __name__ == '__main__': debug()

2、使用PIL库

from PIL import ImageGrab
img = ImageGrab.grab(bbox=(50, 10, 500, 500))
img.save('tmp/filename.png')

https://www.codenong.com/cs105357384/