Home > Mobile >  C# Checking colour on screen error: Parameter must be positive and < Height. (Parameter 'y&#
C# Checking colour on screen error: Parameter must be positive and < Height. (Parameter 'y&#

Time:05-31

My code:

private bool SearchPixel(string hexcode)
    {
        try
        {
            Bitmap bitmap = new Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height);
            Graphics graphics = Graphics.FromImage(bitmap as Image);
            graphics.CopyFromScreen(0, 0, 0, 0, bitmap.Size);

            Color desiredPixelColor = ColorTranslator.FromHtml(hexcode);

            for (int x = 0; x < SystemInformation.VirtualScreen.Width; x  )
            {
                for (int y = 0; x < SystemInformation.VirtualScreen.Height; y  )
                {
                    Color currentPixelColor = bitmap.GetPixel(x, y);
                    label1.Text = "Current colour: "   currentPixelColor;
                    if (desiredPixelColor == currentPixelColor)
                    {

                        label2.Text = "You are on the colour!"
                        return true;

                    }
                }
            }
            label2.Text = "You are not on the colour!";
            return false;
        }
        catch { return false; }
        
    }

    private void timer1_Tick(object sender, EventArgs e)
    {
        SearchPixel("#hexcolourgoeshere");
    }

Error:

System.ArgumentOutOfRangeException: Parameter must be positive and < Height. (Parameter 'y')
   at System.Drawing.Bitmap.GetPixel(Int32 x, Int32 y)
   at coolgame.Form1.SearchPixel(String hexcode) in C:\Users\b\Desktop\Form1.cs:line 27
   at coolgame.Form1.timer1_Tick(Object sender, EventArgs e) in C:\Users\b\Desktop\Form1.cs:line 49
   at System.Windows.Forms.Timer.OnTick(EventArgs e)
   at System.Windows.Forms.Timer.TimerNativeWindow.WndProc(Message& m)
   at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, WM msg, IntPtr wparam, IntPtr lparam)

Tried a lot of things from Google but no luck, same with some things from stackoverflow and nothing worked. I have no idea what is wrong, also the label1 text is always changing to the new colour it works fine it's just the exception is happening every second

CodePudding user response:

I think the error in the second for loop

change :

  x < SystemInformation.VirtualScreen.Height

To :

  y < SystemInformation.VirtualScreen.Height

Like This

for (int x = 0; x < SystemInformation.VirtualScreen.Width; x  )
            {
                for (int y = 0; y < SystemInformation.VirtualScreen.Height; y  )
                {

CodePudding user response:

It's an excellent way to learn how to debug.

The exception is thrown on this line:

Color currentPixelColor = bitmap.GetPixel(x, y);

And it clearly states that the error is on y. If you put your mouse over the y, you'll see 1080 (probably, depending on your screen resolution), which is, indeed, out of the [0, 1079] range.

"But I just checked that it was within the range just before..."

If you put your mouse over SystemInformation.VirtualScreen.Height, you'll see, as expected 1080. But if you put your mouse over <, you'll see that the condition is true. That's not expected...

What are you comparing 1080 to? Put your mouse on the variable and you see... 0. And we saw just before that y was 1080. Then you'll see your typo. In your second for loop, your condition checks the value of x, instead of y.

So you just have to replace

for (int y = 0; x < SystemInformation.VirtualScreen.Height; y  )

with

for (int y = 0; y < SystemInformation.VirtualScreen.Height; y  )

CodePudding user response:

This worked. I don't know if this is the best thing to do, what do you guys think

try
            {
                Bitmap bitmap = new Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height);
                Graphics graphics = Graphics.FromImage(bitmap as Image);
                graphics.CopyFromScreen(0, 0, 0, 0, bitmap.Size);

                Color desiredPixelColor = ColorTranslator.FromHtml(hexcode);

                while (true)
                {
                    for (int x = 0; x < SystemInformation.VirtualScreen.Width; x  )
                    {
                        for (int y = 0; x < SystemInformation.VirtualScreen.Height; y  )
                        {
                            Color currentPixelColor = bitmap.GetPixel(x, y);
                            label1.Text = "Current colour: "   currentPixelColor;
                            if (desiredPixelColor == currentPixelColor)
                            {

                                label2.Text = "On colour : Yes";
                                return true;

                            }
                        }
                    }
                    label2.Text = "On colour: No";
                    return false;
                }
                


            }
            catch { return false; }
  •  Tags:  
  • c#
  • Related