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

时间:2013-11-04 19:34:58

标签: python wxpython wxwidgets

我无法在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()

1 个答案:

答案 0 :(得分:0)

您根本无需致电app.MainLoop(),因为您的程序应在保存图片后立即退出。