我的代碼:
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 的一些東西也一樣,但沒有任何效果。我不知道怎么了
uj5u.com熱心網友回復:
我認為第二個 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 )
{
uj5u.com熱心網友回復:
這是學習如何除錯的好方法。
在這一行拋出例外:
Color currentPixelColor = bitmap.GetPixel(x, y);
它清楚地表明錯誤是 on y。如果您將滑鼠放在 上y,您會看到1080(可能取決于您的螢屏解析度),這確實超出了 [0, 1079] 范圍。
“可是我剛才查了一下,是在剛才的范圍內……”
如果你把滑鼠放在上面SystemInformation.VirtualScreen.Height,你會看到,正如預期的那樣1080。但是如果你把滑鼠放在上面<,你會看到條件是true。這不是預期的...
你拿什么來比較1080?將滑鼠放在變數上,您會看到0...... 我們之前看到的y是1080. 然后你會看到你的錯字。在您的第二個for回圈中,您的條件檢查 的值x,而不是y。
所以你只需要更換
for (int y = 0; x < SystemInformation.VirtualScreen.Height; y )
和
for (int y = 0; y < SystemInformation.VirtualScreen.Height; y )
uj5u.com熱心網友回復:
這行得通。我不知道這是否是最好的做法,你們怎么看
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; }
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/483202.html
標籤:C#
上一篇:無法通過MassTransit中IPublishEndpoint的Publish方法發送自定義物件串列(訊息型別不得為系統型別)
