一段Python实现的全屏抓图代码:
1
#
coding:gb2312
2 # python实现全屏截图(win32)
3
4
5 import time
6 import os, win32gui, win32ui, win32con, win32api
7
8 def window_capture():
9 hwnd = 0
10 hwndDC = win32gui.GetWindowDC(hwnd)
11 mfcDC = win32ui.CreateDCFromHandle(hwndDC)
12 saveDC = mfcDC.CreateCompatibleDC()
13 saveBitMap = win32ui.CreateBitmap()
14 MoniterDev = win32api.EnumDisplayMonitors(None,None)
15 w = MoniterDev[0][ 2 ][ 2 ]
16 h = MoniterDev[0][ 2 ][ 3 ]
17 print w,h
18 saveBitMap.CreateCompatibleBitmap(mfcDC, w, h)
19 saveDC.SelectObject(saveBitMap)
20 saveDC.BitBlt((0,0),(w, h) , mfcDC, (0,0), win32con.SRCCOPY)
21 bmpname = win32api.GetTempFileName( " . " , "" )[0] + ' .bmp '
22 saveBitMap.SaveBitmapFile(saveDC, bmpname)
23 return bmpname
24
25 os.system(window_capture())
2 # python实现全屏截图(win32)
3
4
5 import time
6 import os, win32gui, win32ui, win32con, win32api
7
8 def window_capture():
9 hwnd = 0
10 hwndDC = win32gui.GetWindowDC(hwnd)
11 mfcDC = win32ui.CreateDCFromHandle(hwndDC)
12 saveDC = mfcDC.CreateCompatibleDC()
13 saveBitMap = win32ui.CreateBitmap()
14 MoniterDev = win32api.EnumDisplayMonitors(None,None)
15 w = MoniterDev[0][ 2 ][ 2 ]
16 h = MoniterDev[0][ 2 ][ 3 ]
17 print w,h
18 saveBitMap.CreateCompatibleBitmap(mfcDC, w, h)
19 saveDC.SelectObject(saveBitMap)
20 saveDC.BitBlt((0,0),(w, h) , mfcDC, (0,0), win32con.SRCCOPY)
21 bmpname = win32api.GetTempFileName( " . " , "" )[0] + ' .bmp '
22 saveBitMap.SaveBitmapFile(saveDC, bmpname)
23 return bmpname
24
25 os.system(window_capture())