Search code examples
bitmaprambasic

Ram usage going crazy while using Dim ... as New Bitmap with a timer


i'm making a bot that needs to detects some pixel colors at a few a spots every 100ms. The only way i found was to save the current screen to a bitmap and get those colors from this bitmap. But it seems like each 100ms it creates a new bitmap and don't get rid of the previous one, which constantly increase ram usage !

Here is my code :

 Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
    Dim Img As New Bitmap(1280, 720)
    Dim ImgGraphics As Graphics = Graphics.FromImage(Img)
    ImgGraphics.CopyFromScreen(0, 0, 0, 0, Img.Size)

    Dim Slot1Selec As Color = Img.GetPixel(1158, 572)
    If Slot1Selec.ToArgb = -65906 Then
        Label2.Text = ("1")
    Else : Label2.Text = ("0")
    End If
    Dim Slot1Life As Color = Img.GetPixel(1148, 559)
    If Slot1Life.ToArgb = -13052710 Then
        Label3.Text = ("1")
    Else : Label3.Text = ("0")
    End If

And i'm checking 16 pixel colors, here only 2 but it's just the same thing after with Slot2Selec, Slot2life, Slot3Selec, Slot3Life etc...

Please help ! Else an other way to get a pixel color without creating those stupid bitmaps would help alot, thanks :)


Solution

  • Well i found a fix !

    Forgot those lines at the end :

        ImgGraphics.Dispose()
    

    And voila.