3

I was searching through the posts on this site and i came across this: How do I get the colour of a pixel at X,Y using c#?

would this method still be effective for trying to get the colour of a pixel just inside a form?

If not, what would be a way to essentially "map" the form in an 2D array of color values?

For example, I have a Tron game, and I want to check to see if the next location of the lightbike already contains another lightbike.

Thanks, Ian

Community
  • 1
  • 1
Ian McCullough
  • 1,423
  • 4
  • 25
  • 36

3 Answers3

3
using System;
using System.Drawing;
using System.Runtime.InteropServices;

sealed class Win32
{
    [DllImport("user32.dll")]
    static extern IntPtr GetDC(IntPtr hwnd);

    [DllImport("user32.dll")]
    static extern Int32 ReleaseDC(IntPtr hwnd, IntPtr hdc);

    [DllImport("gdi32.dll")]
    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 & 0x000000FF),
                    (int)(pixel & 0x0000FF00) >> 8,
                    (int)(pixel & 0x00FF0000) >> 16);
       return color;
    }
}

Using this, you can then do:

public static class ControlExts
{
    public static Color GetPixelColor(this Control c, int x, int y)
    {
        var screenCoords = c.PointToScreen(new Point(x, y));
        return Win32.GetPixelColor(screenCoords.X, screenCoords.Y);
    }
}

So, in your case you can do:

var desiredColor = myForm.GetPixelColor(10,10);
Bill
  • 25,119
  • 8
  • 94
  • 125
  • I get an issue with the "return Win32" line. Visual Studio wants to change it to Microsoft.Win32. I am using Visual Studio 2010. Is there a library I have to import or something? – Ian McCullough Apr 13 '12 at 05:02
  • This solution is not clean. The `IntPtr.Zero` will get you the desktop dc and there might be windows on top of your window. Better: get the `Handle` property of the control you want the color from. [Compare](http://stackoverflow.com/a/24759418/1442225) – Bitterblue Aug 04 '14 at 12:23
0

You can use GetPixel method to get the color.

e.g.

// Create a Bitmap object from an image file. Bitmap myBitmap = new Bitmap("Grapes.jpg");

// Get the color of a pixel within myBitmap. Color pixelColor = myBitmap.GetPixel(50, 50);

This could be another way to do it for different situation for detail click here

  using System;
  using System.Drawing;
  using System.Runtime.InteropServices;


 sealed class Win32
  {
      [DllImport("user32.dll")]
      static extern IntPtr GetDC(IntPtr hwnd);

      [DllImport("user32.dll")]
      static extern Int32 ReleaseDC(IntPtr hwnd, IntPtr hdc);

      [DllImport("gdi32.dll")]
      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 & 0x000000FF),
                    (int)(pixel & 0x0000FF00) >> 8,
                    (int)(pixel & 0x00FF0000) >> 16);
       return color;
      }
   }
Community
  • 1
  • 1
Adil
  • 146,340
  • 25
  • 209
  • 204
0

You COULD use the method from the question you referenced to get the colour of a pixel from your form, you'd just need to work out if the pixel was within the bounds of your form first, and you'd need to translate coordinates from your form to the coordinates of the screen and vice versa.

EDIT: After a bit of a think, this would be no good if someone opened another window over the top of your form! Best to figure out a different way of doing it I think...

joshuahealy
  • 3,529
  • 22
  • 29