如何在python中释放内存wx.MemoryDC公司

2024-04-19 01:54:15 发布

您现在位置:Python中文网/ 问答频道 /正文

我不能在IDE中使用脚本两次,我想这是因为内存没有释放。我试着做些什么,但我真的不知道该如何释放它。在

import wx
import win32gui
from ctypes import windll

hwnd = win32gui.FindWindow(None, 'Steam')

# Window_FromHWND appears in wxPython 2.6.2.1+
def getScreenshot():
    window = wx.Window_FromHWND(None, hwnd)

    src_dc = wx.WindowDC(window)
    width, height = window.GetSize()

    mem_dc = wx.MemoryDC()
    bitmap = wx.EmptyBitmap(width, height)
    mem_dc.SelectObject(bitmap)

    windll.user32.PrintWindow(hwnd, mem_dc.GetHDC() , 0)

    mem_dc.SelectObject(wx.NullBitmap)

    mem_dc.Clear()
    src_dc.Clear()
    window.Destroy()
    mem_dc.Destroy()
    src_dc.Destroy()

    return bitmap.ConvertToImage()

app = wx.App(redirect=False)

image = getScreenshot()
filename = "testsnap.png"
image.SaveFile(filename, wx.BITMAP_TYPE_PNG)
image.Destroy()

app.MainLoop()

Tags: imageimportsrcnonedcwindowmemwx