title: High CPU usage when getting color under cursor
body: <p>I'm working on an app, moving the mouse in a certain area and clicking the mouse if something is not the color black.</p>
  <p>However, I am getting a really high CPU usage while using this method too get the color below the cursor. After 5 completed runs from startY to endY - the application is lagging that much it takes around 5-10 sec. to get too the end of the area. With this part commented out, the application runs fine and each run doesn't increase in the too complete.</p>
  <p>Here is my while loop:</p>
  <pre><code>private void moveMouse(int startX, int endX, int startY, int endY)
      {
          int newPosX = startX;
          int newPosY = startY;
          while (running)
          {
              Application.DoEvents();
              //this.Cursor = new Cursor(Cursor.Current.Handle);
              Cursor.Position = new Point(newPosX, newPosY);
              Thread.Sleep(3);
              if (colorCursor.Get(newPosX, newPosY))
              {
                  MyMouse.sendClick();
                  countClicks++;
                  lblStatus.Text = &quot;Klik: &quot; + countClicks;
              }
  
              newPosX += 10;
              if (newPosX &gt; endX)
              {
                  newPosY += 25;
                  newPosX = startX;
              }
              if (newPosY &gt; endY)
              {
                  newPosY = startY;
                  Thread.Sleep(1000);
              }
          }
      }
  </code></pre>
  <p>Color below cursor:</p>
  <pre><code>public class ColorUnderCursor
  {
      [DllImport(&quot;gdi32&quot;)]
      public static extern uint GetPixel(IntPtr hDC, int XPos, int YPos);
  
      //[DllImport(&quot;user32.dll&quot;, CharSet = CharSet.Auto)]
      //public static extern bool GetCursorPos(out POINT pt);
  
      [DllImport(&quot;User32.dll&quot;, CharSet = CharSet.Auto)]
      public static extern IntPtr GetWindowDC(IntPtr hWnd);
  
      public bool Get(int x, int y)
      {
          IntPtr dc = GetWindowDC(IntPtr.Zero);
  
          long color = GetPixel(dc, x, y);
          Color underMouse = Color.FromArgb((int)color);
          if(underMouse != Color.FromArgb(0, 0, 0, 0))
              return true;
  
          return false;
      }
  }
  </code></pre>
  <p>How I can minimize this heavy usage of the CPU.</p>
  <p><strong>Solution:</strong>
  It was my method &quot;Get&quot; which was causing the problem. I solved it by this method below, and running the whole thing inside a backgroundworker.</p>
  <pre><code>public bool GetPixel(Point position)
      {
          using (var bitmap = new Bitmap(1, 1))
          {
              using (var graphics = Graphics.FromImage(bitmap))
              {
                  graphics.CopyFromScreen(position, new Point(0, 0), new Size(1, 1));
              }
              if (bitmap.GetPixel(0, 0) != Color.FromArgb(255, 0, 0, 0) &amp;&amp; bitmap.GetPixel(0, 0) != Color.FromArgb(255, 255, 255, 255))
                  return true;
  
              return false;
          }
      }
  </code></pre>