我有一个计时器,在其他事情中,检查屏幕上的5个点的颜色变化。我的程序监视电话系统应用程序,并检查是否有新的电话从5个按键中的任何一个进入。基于我发布的另一个问题,我使用了以下代码。Monitor an area of the screen for a certain color in Visual Basic
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,但我似乎找不到任何其他方法来做到这一点。
发布于 2012-04-07 04:18:54
调用GetDC(0)一次,将其保存到一个变量,并将该变量传递给ReleaseDC
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参数,hWnd和hDC。
https://stackoverflow.com/questions/10048733
复制相似问题