SolvedToying with C#

Posts 1–3 of 3 · Page 1 of 1
Toying with C#
I am unable to return a point with this block of code always ends up returning Cursor position instead of position new point( x, y) Any help would be appreciated. I'm coding about 4 separate programs simply as a learning experience.


Code:
        public Point FindPixel(Color ColorToFind)
        {
            Bitmap bitmapin = new Bitmap(Screen.PrimaryScreen.Bounds.Width,
Screen.PrimaryScreen.Bounds.Height);
            var gfxScreenshot = Graphics.FromImage(bitmapin);
            gfxScreenshot.CopyFromScreen(Screen.PrimaryScreen.Bounds.X, Screen.PrimaryScreen.Bounds.Y, 0, 0,
   Screen.PrimaryScreen.Bounds.Size, CopyPixelOperation.SourceCopy);

            for (int y = 0; y < (Screen.PrimaryScreen.Bounds.Height; y++)
            {
                for (int x = 0; x < (Screen.PrimaryScreen.Bounds.Width; x++)
                {
                    Color pixel = bitmapin.GetPixel(x, y);
                    if (pixel == ColorToFind)
                    {
                        return new Point(x, y);
                    }
                }
            }
            return new Point(Cursor.Position.X, Cursor.Position.Y);
        }
Problem isn't in get pixel so it is either in my for loop arguments or my return statements.
I don't really know what you're problem is, it's working properly for me.
You had some extra parenthesis which made it not compile.

To really check if it's working, set the cursor position to the new position you found.

Code:
Point p = FindPixel(Color.FromArgb(200, 42, 8));
Cursor.Position = p;
 
Working code

Code:
public Point FindPixel(Color ColorToFind)
        {
            Bitmap bitmapin = new Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height);
            var gfxScreenshot = Graphics.FromImage(bitmapin);
            gfxScreenshot.CopyFromScreen(Screen.PrimaryScreen.Bounds.X, Screen.PrimaryScreen.Bounds.Y, 0, 0, Screen.PrimaryScreen.Bounds.Size, CopyPixelOperation.SourceCopy);

            for (int y = 0; y < Screen.PrimaryScreen.Bounds.Height; y++)
            {
                for (int x = 0; x < Screen.PrimaryScreen.Bounds.Width; x++)
                {
                    Color pixel = bitmapin.GetPixel(x, y);
                    if (pixel == ColorToFind)
                    {
                        return new Point(x, y);
                    }
                }
            }

            return new Point(Cursor.Position.X, Cursor.Position.Y);
        }
Quote Originally Posted by greyb1t View Post
I don't really know what you're problem is, it's working properly for me.
You had some extra parenthesis which made it not compile.

To really check if it's working, set the cursor position to the new position you found.
It compiled fine for me. The problem as stated. ALWAYS returns cursor position. found the problem the problem is the color arguments. Thanks for your help. I used a messagebox displaying the rgb values every step to find out that my windows wont display pure red from microsoft paint. ends up a different red value. news to me.
Posts 1–3 of 3 · Page 1 of 1
This thread is closed for replies.

Similar Threads

Tags for this Thread

None

Talk with us