python使用win32api截图并回收资源
抄了一个截图代码,已经不可考来源了,总之大家都这么截图的,但是很少有把回收资源说清楚。
import win32gui
import win32ui
import win32con
import win32api
def window_capture(filename, wmin, hmin, wmax, hmax):
hwnd = 0 #Desktop
hwndDC = win32gui.GetWindowDC(hwnd)
mfcDC = win32ui.CreateDCFromHandle(hwndDC)
saveDC = mfcDC.CreateCompatibleDC()
BitMap = win32ui.CreateBitmap()
BitMap.CreateCompatibleBitmap(mfcDC, wmax-wmin, hmax-hmin)
saveDC.SelectObject(BitMap)
saveDC.BitBlt((0, 0), (wmax, hmax), mfcDC, (wmin, hmin), win32con.SRCCOPY)
BitMap.SaveBitmapFile(saveDC, filename)
win32gui.DeleteObject(BitMap.GetHandle())
saveDC.DeleteDC()
mfcDC.DeleteDC()
win32gui.ReleaseDC(hwnd, hwndDC)
注意按照顺序回收资源