r/csharp•
roller344

How har will this be to make?

I'm new to programming, but i need to program something for a project of mine. I need to make a program that can track a spot of my monitor, then when it notices change i that spot it will start a timer, and when it notices change again it will stop the timer and give me the number. Will this be hard? i have done very little C# programming.

Posted Mar 11, 2015, 6:28 PM UTC

020
0xffff2•

Ignore the other guy. What you're trying to do isn't hard to understand at all.

It shouldn't be too hard to implement, but it really depends on you. You can easily search to find methods of getting the color of a specific pixel on the screen, set up a timer, etc. The only potentially hard part is gluing it all together.

3
roller344OP•

Could you lead me on my way, it basicly just has to check the color of the pixel like 30 times a second, and when the color is white it will start the timer and when it turns white again it should stop. Do you know where i should start, or a good tutorial maybe.

2
roller344OP•

I found this.

https://msdn.microsoft.com/en-us/library/system.drawing.bitmap.getpixel%28v=vs.110%29.aspx

Now i will just have to add the coordinates, add some if and then commands, and thats it right?

2
kryzchek•

That's only going to work if you're looking to get the color of a pixel at a certain co-ordinate within a Bitmap.

Now, you can copy the screen to a Bitmap, you'd need to do THIS multiple times per second and then check the pixel in the screen image. You're better off using P/invoke to use the Windows API to do this.

2
Noxio•
1
roller344OP•

Yes this is what i want. Now it's just how to make it work, the process should be.

  1. get the color of the pixel and save it.

  2. Check color of pixel and save color

  3. check color of the pixel 30 times a sec or more.

  4. if color is the original color dont do anything

  5. if color is not original color start timer

  6. keep checking till the non original color appears again and stop timer.

Now to make it into progamming language....

1
Noxio•

See the last link SO has some sample code....

1
[deleted]•

[deleted]

1
roller344OP•

private void PollPixel(Point location, Color color) { while(true) { var c = GetColorAt(location);

    if (c.R == color.R && c.G == color.G && c.B == color.B)
    {
        DoAction();
        return;
    }

    // By calling Thread.Sleep() without a parameter, we are signaling to the
    // operating system that we only want to sleep long enough for other
    // applications.  As soon as the other apps yield their CPU time, we will
    // regain control.
    Thread.Sleep()
}

}

Can this be used?

1
Noxio•

If I understand what it is you want correctly then yes that would work albeit in a CPU hungry way. If you have a GUI you will want a separate thread for the poll otherwise the while loop will block it.

Also note that Color has an equals for testing equivalence.

1
ElMalakai•

Skip C#, use AutoHotKey. You'll have something running in seconds.

Also, there's some nice AHK libraries that do more than just pixel reads. You can pass them images and they'll find them on a given rectangle on any of your screens.

1
roller344OP•

I will take a look at this, thanks.

1
jerzky•

i actually made a program that does exactly this a while back. i could give you the code if u like or i can offer you my help if you rather want to solve this yourself

1
roller344OP•

Would be really nice if you could give me the code :), i realize i dont have the time and experience to make this.

Edit: im going to bed, i hope i can talk to you tomorrow, if you have the code it would really save my life :)

1
NaughtyMonster•

Is it just a standard windows desktop app or is it a 3d accelerated application/game you're trying to monitor?

1
Athirux•

Well it's hard to understand what you want to achieve.

-1
roller344OP•

A program that tracked a sertain part of my screen, its all black, when the program detects change in color it will start a timer, when there is a change again it will stop the timer. Cant really explain it better.

1