繁体   English   中英

C# 检查屏幕上的颜色错误:参数必须为正且 < 高度。 (参数'y')

[英]C# Checking colour on screen error: Parameter must be positive and < Height. (Parameter 'y')

我的代码:

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");
    }

错误:

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)

尝试了 Google 的很多东西,但没有运气,stackoverflow 的一些东西也一样,但没有任何效果。 我不知道怎么了

我认为第二个 for 循环中的错误

改变 :

  x < SystemInformation.VirtualScreen.Height

至 :

  y < SystemInformation.VirtualScreen.Height

像这样

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

这是学习如何调试的好方法。

在这一行抛出异常:

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

它清楚地表明错误在y上。 如果您将鼠标放在y上,您将看到1080 (可能取决于您的屏幕分辨率),这实际上超出了 [0, 1079] 范围。

“可是我刚才查了一下,是在刚才的范围内……”

如果您将鼠标放在SystemInformation.VirtualScreen.Height上,您会看到如预期的那样1080 但是,如果您将鼠标放在<上,您会看到条件为true 不是预期的...

你把1080和什么比较? 将鼠标放在变量上,您会看到... 0 我们之前看到y1080 然后你会看到你的错字。 在您的第二个for循环中,您的条件检查x的值,而不是y

所以你只需要更换

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

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

这行得通。 我不知道这是否是最好的做法,你们怎么看

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; }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM