title: C# get pixel color from another window
body: <p>I want to get a pixel color from another window. The code I have is:</p>
  <pre class="lang-cs prettyprint-override"><code>using System;
  using System.Drawing;
  using System.Runtime.InteropServices;
  
  sealed class Win32
  {
      [DllImport(&quot;user32.dll&quot;)]
      static extern IntPtr GetDC(IntPtr hwnd);
      
      [DllImport(&quot;user32.dll&quot;)]
      static extern Int32 ReleaseDC(IntPtr hwnd, IntPtr hdc);
      
      [DllImport(&quot;gdi32.dll&quot;)]
      static extern uint GetPixel(IntPtr hdc, int nXPos, int nYPos);
      
      static public System.Drawing.Color GetPixelColor(int x, int y)
      {
          IntPtr hdc = GetDC(IntPtr.Zero);
          uint pixel = GetPixel(hdc, x, y);
          ReleaseDC(IntPtr.Zero, hdc);
          Color color = Color.FromArgb((int)(pixel &amp; 0x000000FF),
              (int)(pixel &amp; 0x0000FF00) &gt;&gt; 8,
              (int)(pixel &amp; 0x00FF0000) &gt;&gt; 16);
          return color;
      }
  }
  </code></pre>
  <p>The problem is that this code is scanning the whole screen which is not what I want. The idea is to modify the code to scan for pixel color based on another application screen boundaries. Maybe something with <code>FindWindow</code> or <code>GetWindow</code>? Thanks in advance.</p>