954,500 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Get coordinates of pixel color

Hi,

I was wondering how I can get the coordinates of a specific color from a window? Is it possible.

I did some research, but I only found to get a color from coordinates, but I need it the other way around.

Atm I have this, I don't know if it is right, cause I usually program in java.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;

namespace ConsoleApplication2
{
    class Program
    {

        [DllImport("user32.dll", CharSet = CharSet.Auto)]
        public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);

        [DllImport("user32.dll", EntryPoint = "GetDC")]
        public static extern IntPtr GetDC(IntPtr ptr);
            
        static void Main(string[] args)
        {

            IntPtr nWnd = FindWindow("","DutchLeader - Uitnodigingen - Windows Internet Explorer");
            if (nWnd != IntPtr.Zero)
            {
                IntPtr dcWindow = GetDC(nWnd);
                //Here should come the code to get the coordinates from the color pixel
            }
            else
            {
                Console.WriteLine("Window niet gevonden");
            }
        }
    }
}


The second problem is that he can't findow the window. The window he needs to get, can be seen in photo which is in attachment.

Grtz

Attachments Troubles.jpg 14.43KB
Kligham
Light Poster
36 posts since Jun 2009
Reputation Points: 10
Solved Threads: 0
 

>how I can get the coordinates of a specific color from a window?

Take a look at codeproject article - http://www.codeproject.com/KB/miscctrl/ColorGET.aspx

__avd
Posting Genius (adatapost)
Moderator
8,648 posts since Oct 2008
Reputation Points: 2,136
Solved Threads: 1,241
 
pauldani
Light Poster
49 posts since Jan 2010
Reputation Points: 8
Solved Threads: 7
 

I think you probably misunderstood me. What I need is when I define the RGB values, the program should look for them and give me the coordinates of where it is found. Me/The program doesn't know a coordinate, it needs to search for it.

For example:

Red: 85
Green: 23
Blue: 240

Then program needs to search of the window (like in code)

Kligham
Light Poster
36 posts since Jun 2009
Reputation Points: 10
Solved Threads: 0
 

A BitMap has a GetPixel method which returns a Color struct for the Color at coordinates X Y in the Bitmap. A Color struct can Compare one Color with another. So loop through every pixel of your bitmap and test for your color. If found you got your desired position.

ddanbe
Senior Poster
3,829 posts since Oct 2008
Reputation Points: 2,070
Solved Threads: 661
 

There is a problem with this, very seldom will there be only 1 pixel of a particular color anywhere, It is very l likely that you will get hundreds of coordinates with even an unpopular color as many images contain millions of colors to make up and image.

but I digress a good approach would be to capture the entire screen as a bitmap (assuming you want screen coordinates) and then loop through that bitmap using getpixel and compare its color to your color, and then add that coordinate to your List of coordinates.

if you just wanted to look in a particular window, you can still screen cap the whole screen but just loop through a rectangle that is bound to that windows position on screen.


although, I can't see the use for this. Best of luck.

Diamonddrake
Master Poster
724 posts since Mar 2008
Reputation Points: 442
Solved Threads: 89
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: