2015-07-28 91 views
1

我是新程序员,尝试使用c#编写一个程序,该程序点击屏幕上的特定坐标并从屏幕搜索图像,并在找到它时单击它。程序工作正常,但30-40分钟后崩溃。当碰撞窗弹出 - 未处理的异常发生在您的应用程序....未处理的异常:参数无效

参数无效

下面是详细介绍:

See the end of this message for details on invoking 
just-in-time (JIT) debugging instead of this dialog box. 

************** Exception Text ************** 
System.ArgumentException: Parameter is not valid. 
    at System.Drawing.Bitmap..ctor(Int32 width, Int32 height, PixelFormat format) 
    at System.Drawing.Bitmap..ctor(Int32 width, Int32 height) 
    at Uncle_s_Bot.Form1.Screenshot() 
    at Uncle_s_Bot.Form1.btnStart_Click(Object sender, EventArgs e) 
    at System.Windows.Forms.Control.OnClick(EventArgs e) 
    at System.Windows.Forms.Button.OnClick(EventArgs e) 
    at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent) 
    at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks) 
    at System.Windows.Forms.Control.WndProc(Message& m) 
    at System.Windows.Forms.ButtonBase.WndProc(Message& m) 
    at System.Windows.Forms.Button.WndProc(Message& m) 
    at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m) 
    at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m) 
    at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam) 

这里和程序的代码:

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 
using System.Windows.Forms; 
using System.Runtime.InteropServices; 
using System.Threading; 

namespace Uncle_s_Bot 
{ 
    public partial class Form1 : Form 
    { 
     [DllImport("user32.dll")] 
     static extern void mouse_event(uint dwFlags, uint dx, uint dy, uint dwData, 
     int dwExtraInfo); 

     public enum MouseEventFlags : uint 
     { 
      MOUSEEVENTF_ABSOLUTE = 0x8000, 
      MOUSEEVENTF_LEFTDOWN = 0x0002, 
      MOUSEEVENTF_LEFTUP = 0x0004, 
      MOUSEEVENTF_MIDDLEDOWN = 0x0020, 
      MOUSEEVENTF_MIDDLEUP = 0x0040, 
      MOUSEEVENTF_MOVE = 0x0001, 
      MOUSEEVENTF_RIGHTDOWN = 0x0008, 
      MOUSEEVENTF_RIGHTUP = 0x0010, 
      MOUSEEVENTF_XDOWN = 0x0080, 
      MOUSEEVENTF_XUP = 0x0100, 
      MOUSEEVENTF_WHEEL = 0x0800, 
      MOUSEEVENTF_HWHEEL = 0x01000 
     } 

     public Form1() 
     { 
      InitializeComponent(); 
     } 

     private void btnStart_Click(object sender, EventArgs e) 
     { 
      Bitmap bmpScreenshot; 
      Point location; 
      bool success; 
      bool MaybeAvailable = false; 


      while (true) 
      { 
       // Point 1 
       do 
       { 
        bmpScreenshot = Screenshot(); 
        success = FindBitmap(Properties.Resources.bmp_Bonus, bmpScreenshot, out location); 
        if (success == true) 
        { 
         Cursor.Position = location; 
         mouseClick(); 
         Thread.Sleep(1500); 
         MaybeAvailable = true; 
        } 
        else 
        { 
         Cursor.Position = new Point(95, 140); 
         mouseClick(); 
         Thread.Sleep(7500); 
         MaybeAvailable = false; 
        } 
       } 
       while (MaybeAvailable); 

       // Point 2 
       do 
       { 
        bmpScreenshot = Screenshot(); 
        success = FindBitmap(Properties.Resources.bmp_Bonus, bmpScreenshot, out location); 
        if (success == true) 
        { 
         Cursor.Position = location; 
         mouseClick(); 
         Thread.Sleep(1500); 
         MaybeAvailable = true; 
        } 
        else 
        { 
         Cursor.Position = new Point(135, 135); 
         mouseClick(); 
         Thread.Sleep(10000); 
         MaybeAvailable = false; 
        } 
       } 
       while (MaybeAvailable); 

       // Point 3 
       do 
       { 
        bmpScreenshot = Screenshot(); 
        success = FindBitmap(Properties.Resources.bmp_Bonus, bmpScreenshot, out location); 
        if (success == true) 
        { 
         Cursor.Position = location; 
         mouseClick(); 
         Thread.Sleep(1500); 
         MaybeAvailable = true; 
        } 
        else 
        { 
         Cursor.Position = new Point(180, 140); 
         mouseClick(); 
         Thread.Sleep(10000); 
         MaybeAvailable = false; 
        } 
       } 
       while (MaybeAvailable); 

       // Point 4 
       do 
       { 
        bmpScreenshot = Screenshot(); 
        success = FindBitmap(Properties.Resources.bmp_Bonus, bmpScreenshot, out location); 
        if (success == true) 
        { 
         Cursor.Position = location; 
         mouseClick(); 
         Thread.Sleep(1500); 
         MaybeAvailable = true; 
        } 
        else 
        { 
         Cursor.Position = new Point(190, 170); 
         mouseClick(); 
         Thread.Sleep(8500); 
         MaybeAvailable = false; 
        } 
       } 
       while (MaybeAvailable); 

       // Point 5 
       do 
       { 
        bmpScreenshot = Screenshot(); 
        success = FindBitmap(Properties.Resources.bmp_Bonus, bmpScreenshot, out location); 
        if (success == true) 
        { 
         Cursor.Position = location; 
         mouseClick(); 
         Thread.Sleep(1500); 
         MaybeAvailable = true; 
        } 
        else 
        { 
         Cursor.Position = new Point(150, 185); 
         mouseClick(); 
         Thread.Sleep(10000); 
         MaybeAvailable = false; 
        } 
       } 
       while (MaybeAvailable); 

       // Point 6 
       do 
       { 
        bmpScreenshot = Screenshot(); 
        success = FindBitmap(Properties.Resources.bmp_Bonus, bmpScreenshot, out location); 
        if (success == true) 
        { 
         Cursor.Position = location; 
         mouseClick(); 
         Thread.Sleep(1500); 
         MaybeAvailable = true; 
        } 
        else 
        { 
         Cursor.Position = new Point(180, 210); 
         mouseClick(); 
         Thread.Sleep(10000); 
         MaybeAvailable = false; 
        } 
       } 
       while (MaybeAvailable); 

       // Point 7 
       do 
       { 
        bmpScreenshot = Screenshot(); 
        success = FindBitmap(Properties.Resources.bmp_Bonus, bmpScreenshot, out location); 
        if (success == true) 
        { 
         Cursor.Position = location; 
         mouseClick(); 
         Thread.Sleep(1500); 
         MaybeAvailable = true; 
        } 
        else 
        { 
         Cursor.Position = new Point(150, 240); 
         mouseClick(); 
         Thread.Sleep(8500); 
         MaybeAvailable = false; 
        } 
       } 
       while (MaybeAvailable); 

       // Point 8 
       do 
       { 
        bmpScreenshot = Screenshot(); 
        success = FindBitmap(Properties.Resources.bmp_Bonus, bmpScreenshot, out location); 
        if (success == true) 
        { 
         Cursor.Position = location; 
         mouseClick(); 
         Thread.Sleep(1500); 
         MaybeAvailable = true; 
        } 
        else 
        { 
         Cursor.Position = new Point(95, 235); 
         mouseClick(); 
         Thread.Sleep(9500); 
         MaybeAvailable = false; 
        } 
       } 
       while (MaybeAvailable); 

       // Point 9 
       do 
       { 
        bmpScreenshot = Screenshot(); 
        success = FindBitmap(Properties.Resources.bmp_Bonus, bmpScreenshot, out location); 
        if (success == true) 
        { 
         Cursor.Position = location; 
         mouseClick(); 
         Thread.Sleep(1500); 
         MaybeAvailable = true; 
        } 
        else 
        { 
         Cursor.Position = new Point(110, 200); 
         mouseClick(); 
         Thread.Sleep(10000); 
         MaybeAvailable = false; 
        } 
       } 
       while (MaybeAvailable); 

       // Point 10 
       do 
       { 
        bmpScreenshot = Screenshot(); 
        success = FindBitmap(Properties.Resources.bmp_Bonus, bmpScreenshot, out location); 
        if (success == true) 
        { 
         Cursor.Position = location; 
         mouseClick(); 
         Thread.Sleep(1500); 
         MaybeAvailable = true; 
        } 
        else 
        { 
         Cursor.Position = new Point(80, 170); 
         mouseClick(); 
         Thread.Sleep(10000); 
         MaybeAvailable = false; 
        } 
       } 
       while (MaybeAvailable); 

       // Point 11 
       do 
       { 
        bmpScreenshot = Screenshot(); 
        success = FindBitmap(Properties.Resources.bmp_Bonus, bmpScreenshot, out location); 
        if (success == true) 
        { 
         Cursor.Position = location; 
         mouseClick(); 
         Thread.Sleep(1500); 
         MaybeAvailable = true; 
        } 
        else 
        { 
         Cursor.Position = new Point(120, 165); 
         mouseClick(); 
         Thread.Sleep(11500); 
         MaybeAvailable = false; 
        } 
       } 
       while (MaybeAvailable); 
      } 

     } 

     private void mouseClick() 
     { 
      mouse_event((uint)MouseEventFlags.MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0); 
      Thread.Sleep((new Random()).Next(20, 30)); 
      mouse_event((uint)MouseEventFlags.MOUSEEVENTF_LEFTUP, 0, 0, 0, 0); 
     } 

     private Bitmap Screenshot() 
     { 
      // save snapshot of screen 
      Bitmap bmpScreenshot = new Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height); 

      // create a graphic object so we can draw the screen in the bitmap 
      Graphics g = Graphics.FromImage(bmpScreenshot); 

      // copy from screen into the bitmap we created 
      g.CopyFromScreen(0, 0, 0, 0, Screen.PrimaryScreen.Bounds.Size); 

      // return the screenshot 
      return bmpScreenshot; 
     } 

     // Find location of a bitmap within another bitmap and return if it was succesufulyy found 
     private bool FindBitmap(Bitmap bmpNeedle, Bitmap bmpHaystack, out Point location) 
     { 
      for (int outerX = 0; outerX < bmpHaystack.Width - bmpNeedle.Width; outerX++) 
      { 
       for (int outerY = 0; outerY < bmpHaystack.Height - bmpNeedle.Height; outerY++) 
       { 
        for (int innerX = 0; innerX < bmpNeedle.Width; innerX++) 
        { 
         for (int innerY = 0; innerY < bmpNeedle.Height; innerY++) 
         { 
          Color cNeedle = bmpNeedle.GetPixel(innerX, innerY); 
          Color cHaystack = bmpHaystack.GetPixel(innerX + outerX, innerY + outerY); 

          if (cNeedle.R != cHaystack.R || cNeedle.G != cHaystack.G || cNeedle.B != cHaystack.B) 
          { 
           goto notFound; 
          } 
         } 
        } 
        location = new Point(outerX, outerY); 
        return true; 
       notFound: 
        continue; 
       } 
      } 
      location = Point.Empty; 
      return false; 
     } 

     private void Form1_Load(object sender, EventArgs e) 
     { 

     } 
    } 
} 
+1

您似乎在那里有很多无限循环的可能性,您还应该缩小您的代码示例以便与您的问题相关。 (在你的代码中有几个突出的错误,我可以看到,但我不知道它们是否与你的问题有关,即不在''Screenshot中配置位图) – Sayse

+0

so ..哪一行触发异常? –

+0

我让他们做while循环,因为我没有多少次会找到图像。如果我知道问题究竟在哪里,我只会发布那一部分,但我不知道。 – UncleScroogeACE

回答

0

创建新的位图,图形导致内部GDI +函​​数调用。所有这些函数最终都会有错误,并且错误将转换为.NET异常(不幸的是,没有附加参数名称,信息等)。由于这个原因,一些错误可能会很难理解。

由于处理泄漏和内存泄漏(在GDI +的情况下),可能会发生一些问题。例如,创建大量非常大的位图会在非托管空间中分配大量连续的块。 GC不知道内存压力,不会尝试清理。假设100次调用可能会注意到内存不足,并且抱怨引发ArgumentException的宽度和高度过大,则将Width和Height传递足够大。

首先,请考虑处置位图,如果你不需要它们。 不要忘记处理图形。

如果它没有帮助,请让我知道。

+0

我不再需要它们时处理所有的位图和图形,并测试程序2小时,并正常工作。今晚我会试试更多时间,看看会发生什么。谢谢! – UncleScroogeACE