System resource hog

D

Daniel N

I am using a timer to capture the screen, and verify certain criteria
through GetPixel(). The timer is set at 333 (about one third of a second).
And after running the program, it takes MASSIVE resources and slows the
computer down. Is there something I can do to prevent this? Here is the code
I am using:



I am using a program to capture the screen, and saving it in an image box;





'Capture Screen

CaptureScreen()

ScreenPictureBox.Image = oBackground



Then saving the image to a bitmap;



'Save save image to bitmap



Dim B As New Bitmap(ScreenPictureBox.Image)



I then GetPixel to determine certain procedures;



'Clock

RGB = B.GetPixel(967, 512)

str = RGB.ToString

str = str.Substring(7, str.Length - 8)

If str = "A=255, R=8, G=8, B=8" Then

ActionLabel.Text = "Clock"

End If



'Player 1

RGB = B.GetPixel(818, 123)

str = RGB.ToString

str = str.Substring(7, str.Length - 8)

If str = "A=255, R=0, G=0, B=0" Then

Label18.Text = "Player 1"

End If



.. . .
 
?

=?ISO-8859-1?Q?G=F6ran_Andersson?=

So you capture the entire screen, put the bitmap in a PictureBox, then
copy the bitmap into a new bitmap?

Why don't you just capture the part of the screen that you are
interrested in?
 
G

Guest

Daniel,

I have no idea what the problem is, but why do you convert the color to a
string and the compare strings if you could check the properties of the
colors like this:

Dim bm As Bitmap = Me.BackgroundImage
Dim col As Color = bm.GetPixel(100, 100)
If col.A = 255 AndAlso col.B = 8 AndAlso col.G = 8 AndAlso col.R = 8
Then
' do something
End If

Also, when working with disposable object, like bitmap objects, make sure
that you do not forget to dispôse them if you do not need them anymore. Like
this:

Dim bm As Bitmap = Me.BackgroundImage
Try
' do something with the bitmap
Finally
bm.Dispose()
End

Also, you could (but is not recommended) start a garbage collection after
you finish your resource comsuming operations with GC.Collect. However the
garbage collector ususally runs automaticaaly when resources are needed, so
ususally this is not necessary.

Hope this helps,

Joris
 
G

Guest

Hm... Running a garbage collection three times a second, that should
quite efficiently kill performance. ;)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top