I have an auto-clicker where if you click a button it checks your current screen, the screen it saw when you made the auto-clicker program, compares them, and if they're similar enough it "approves" the set-up. Otherwise it says to adjust your camera angle and warns you that the auto-clicker could fail.
The problem is if you spam the button, "Calibrate" in this case, it will eventually lead to an "Out of memory" exception.
I noticed this when I got mad running it myself, unable to get it to pass, spamming the button... and then the program crashed.
The code that I think is relevant is:
For Each scr As Screen In Screen.AllScreens
Wdt += scr.Bounds.Width
Next
Dim ScreenSizeTotal As Size = New Size(Wdt, My.Computer.Screen.Bounds.Height)
Dim ScreenGrab1 As New Bitmap(Wdt, My.Computer.Screen.Bounds.Height)
Dim g As Graphics = Graphics.FromImage(ScreenGrab1)
g.CopyFromScreen(New Point(0, 0), New Point(0, 0), ScreenSizeTotal)
If System.IO.File.Exists(DriveLetter & "RSAutoClicker\" & "TestClick.bmp") = True Then
My.Computer.FileSystem.DeleteFile(DriveLetter & "RSAutoClicker\" & "TestClick.bmp")
End If
ScreenGrab1.Save(DriveLetter & "RSAutoClicker\" & "TestClick.bmp")
a = New Bitmap(DriveLetter & "RSAutoClicker\" & TestProgram & "\" & TestProgram & "Click1.bmp") 'Out of memory exception occurs on this line
b = New Bitmap(DriveLetter & "RSAutoClicker\" & "TestClick.bmp")
Dim ScreenRegion As New Bitmap(b.Width, b.Height, System.Drawing.Imaging.PixelFormat.Format32bppPArgb)
For y As Integer = My.Settings.VerifyScreenYU To My.Settings.VerifyScreenYD - 1 Step 4
For x = My.Settings.VerifyScreenXL To My.Settings.VerifyScreenXR - 1 Step 4
Dim aColor As Color = a.GetPixel(x, y)
Dim bColor As Color = b.GetPixel(x, y)
If aColor.ToArgb() = bColor.ToArgb() Then
PixelMatchCount += 1
End If
TotalPixels += 1
Next
Next
a.Dispose()
b.Dispose()
The stuff about saving/deleting is about the "Calibrate" button and deleting the picture after the check has been done. The YL YR XD & XU is the boundaries of where to check in the left, right, top, and down direction on the screen.
The "Step 4" stuff is because I'm only checking every 4th pixel, otherwise its very intensive operating wise. Every 4 pixels in the X & Y direction seemed to be a good compromise that still gave good results.
I use .Dispose() to throw everything away when it's done. But I think I have misunderstood what .Dispose() is for, because it isn't doing that. I feel that is where the error lies, but I don't know how to fix it.
I could be totally wrong, but any help is appreciated. thank you!