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

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)

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.

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.