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)
上一篇:Illustrator(AI)模仿设计出一个逼真的色彩斑澜的U盘实例教程


下一篇:Git:Terminal is dumb, but EDITOR unset