title: How to get screen pixel color the fastest way (C#)
body: <p>I know there are few of these "how to get screen pixel color?" questions, but when I try their solutions I'm not getting good enough results.</p>
  
  <p>I'm making an application that detects colors of 4 different pixels again and again and processes results. The problem is that when I try following code it's able to run just few loops per second and I need at least 100 loops per seconds (that means 400 pixel detections per second).</p>
  
  <pre><code>[DllImport("user32.dll", SetLastError = true)]
  public static extern IntPtr GetDesktopWindow();
  [DllImport("user32.dll", SetLastError = true)]
  public static extern IntPtr GetWindowDC(IntPtr window);
  [DllImport("gdi32.dll", SetLastError = true)]
  public static extern uint GetPixel(IntPtr dc, int x, int y);
  [DllImport("user32.dll", SetLastError = true)]
  public static extern int ReleaseDC(IntPtr window, IntPtr dc);
  
  private Color GetColorAt(int x, int y)
      {
          IntPtr desk = GetDesktopWindow();
          IntPtr dc = GetWindowDC(desk);
          int a = (int)GetPixel(dc, x, y);
          ReleaseDC(desk, dc);
          return Color.FromArgb(255, (a &gt;&gt; 0) &amp; 0xff, (a &gt;&gt; 8) &amp; 0xff, (a &gt;&gt; 16) &amp; 0xff);
      }
  
  private void record()
      {
          sw.Start();
          while(isRunning)
          {
              Color cK1 = GetColorAt(1857, 488);
              Color cK2 = GetColorAt(1857, 556);
              Color cM1 = GetColorAt(1857, 624);
              Color cM2 = GetColorAt(1857, 692);
  
              if (cK1.R + cK1.G + cK1.B &gt; 750 &amp;&amp; !k1pressed)
              {
                  k1pressed = true;
                  addEvent("DOWN", "K1");
              }
              else if (cK1.R + cK1.G + cK1.B &lt; 30 &amp;&amp; k1pressed)
              {
                  k1pressed = false;
                  addEvent("UP", "K1");
              }
  
              if (cK2.R + cK2.G + cK2.B &gt; 750 &amp;&amp; !k2pressed)
              {
                  k2pressed = true;
                  addEvent("DOWN", "K2");
              }
              else if (cK2.R + cK2.G + cK2.B &lt; 30 &amp;&amp; k2pressed)
              {
                  k2pressed = false;
                  addEvent("UP", "K2");
              }
  
              if (cM1.R + cM1.G + cM1.B &gt; 750 &amp;&amp; !m1pressed)
              {
                  m1pressed = true;
                  addEvent("DOWN", "M1");
              }
              else if (cM1.R + cM1.G + cM1.B &lt; 30 &amp;&amp; m1pressed)
              {
                  m1pressed = false;
                  addEvent("UP", "M1");
              }
  
              if (cM2.R + cM2.G + cM2.B &gt; 750 &amp;&amp; !m2pressed)
              {
                  m2pressed = true;
                  addEvent("DOWN", "M2");
              }
              else if (cM2.R + cM2.G + cM2.B &lt; 30 &amp;&amp; m2pressed)
              {
                  m2pressed = false;
                  addEvent("UP", "M2");
              }
              labelStatus.Text = "Recording: " + sw.ElapsedMilliseconds.ToString();
              Application.DoEvents();
          }
      }
  </code></pre>
  
  <p>For explanation, my application captures 4 pixels and each of these pixels represents one (virtual) keyboard key or mouse buttons (let's say A,B,LMB,RMB) and addEvent(str, str) just puts information about key being pressed or released into string and after recording is stopped it saves the string into file.</p>
  
  <p>Is there any way I could do something like this 100 times per second? Because I thought operating with just 4 pixels should be really fast..</p>