首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在Visual Basic中使用GetPixel/GetDC进行内存泄漏

在Visual Basic中使用GetPixel/GetDC进行内存泄漏
EN

Stack Overflow用户
提问于 2012-04-07 04:12:06
回答 1查看 1.2K关注 0票数 0

我有一个计时器,在其他事情中,检查屏幕上的5个点的颜色变化。我的程序监视电话系统应用程序,并检查是否有新的电话从5个按键中的任何一个进入。基于我发布的另一个问题,我使用了以下代码。Monitor an area of the screen for a certain color in Visual Basic

代码语言:javascript
复制
Private Function CheckforCall()
    Try
        Dim queue1 As Integer = GetPixel(GetDC(0), 40, 573)
        Dim queue2 As Integer = GetPixel(GetDC(0), 140, 573)
        Dim queue3 As Integer = GetPixel(GetDC(0), 240, 573)
        Dim queue4 As Integer = GetPixel(GetDC(0), 340, 573)
        Dim queue5 As Integer = GetPixel(GetDC(0), 440, 573)
        ReleaseDC(0)

    <code snipped - Checks to see if the pixel color matches and 
       returns true or false>

    Catch ex As Exception
        Return False
    End Try
End Function

使用这段代码,GDI对象在短时间内迅速增长,抛出了一个OutOfMemory异常。我假设我没有正确地释放DC,但我似乎找不到任何其他方法来做到这一点。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-04-07 04:18:54

调用GetDC(0)一次,将其保存到一个变量,并将该变量传递给ReleaseDC

代码语言:javascript
复制
Dim hDC As IntPtr = GetDC(0)
Try
    Dim queue1 As Integer = GetPixel(hDC, 40, 573)
    Dim queue2 As Integer = GetPixel(hDC, 140, 573)
    Dim queue3 As Integer = GetPixel(hDC, 240, 573)
    Dim queue4 As Integer = GetPixel(hDC, 340, 573)
    Dim queue5 As Integer = GetPixel(hDC, 440, 573)
    ...
Catch ex As Exception
    Return False
Finally
    ReleaseDC(0, hDC)
End Try

注意,ReleaseDC有两个IntPtr参数,hWndhDC

票数 5
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/10048733

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档