Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8040405
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T03:53:04+00:00 2026-06-05T03:53:04+00:00

I am looking for a way of creating a program that will perform a

  • 0

I am looking for a way of creating a program that will perform a mouse click where it finds a certain color on the screen.

For example if there is a red box on the screen, I would want the program to click on the red box in the center of it.

How could I accomplish this in C#?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T03:53:07+00:00Added an answer on June 5, 2026 at 3:53 am

    As you only wanted a general way, I didn’t really make it perfect, but here is the idea:

    Have a method for taking a screen shot:

    public Bitmap ScreenShot()
    {
        var screenShot = new Bitmap(Screen.PrimaryScreen.Bounds.Width,
                                    Screen.PrimaryScreen.Bounds.Height,
                                    PixelFormat.Format32bppArgb);
    
        using (var g = Graphics.FromImage(screenShot))
        {
            g.CopyFromScreen(0, 0, 0, 0, Screen.PrimaryScreen.Bounds.Size);
        }
    
        return screenShot;
    }
    

    And a method to find a certain color in a bitmap:
    Note that this implementation can be DRASTICALLY improved using unsafe code and LockBits (read here and here).

    public Point? GetFirstPixel(Bitmap bitmap, Color color)
    {
        for (var y = 0; y < bitmap.Height; y++)
        {
            for (var x = 0; x < bitmap.Width; x++)
            {
                if (bitmap.GetPixel(x, y).Equals(color))
                {
                    return new Point(x, y);
                }
            }
        }
    
        return null;
    }
    

    Another method you’ll need is one for clicking a certain point:

    [DllImport("user32.dll",
               CharSet=CharSet.Auto,
               CallingConvention=CallingConvention.StdCall)]
    private static extern void mouse_event(long dwFlags,
                                          long dx,
                                          long dy,
                                          long cButtons,
                                          long dwExtraInfo);
    
    private const int MOUSEEVENTF_LEFTDOWN = 0x02;
    private const int MOUSEEVENTF_LEFTUP = 0x04;
    
    public void Click(Point pt)
    {
        Cursor.Position = pt;
        mouse_event(MOUSEEVENTF_LEFTDOWN | MOUSEEVENTF_LEFTUP, pt.X, pt.Y, 0, 0);
    }
    

    And finally, one to wrap it all up:

    public bool ClickOnFirstPixel(Color color)
    {
        var pt = GetFirstPixel(ScreenShot(), color);
    
        if (pt.HasValue)
        {
            Click(pt.Value);
        }
    
        // return whether found pixel and clicked it
        return pt.HasValue;
    }
    

    Then, the usage would be:

    if (ClickOnFirstPixel(Color.Red))
    {
        Console.WriteLine("Found a red pixel and clicked it!");
    }
    else
    {
        Console.WriteLine("Didn't find a red pixel, didn't click.");
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I was looking for a way of creating a collaborative translation widget. So I
First, I'm not looking a way to parse an RSS-feed. I am creating an
I'm creating a website with nodejs+mongoose+mongodb. I'm looking for a good way to initialize
My problem is that I was looking for way to use both storyboard and
I'm looking for a way to check to see that a window I am
I'm creating a program which creates several panels with randomly determined colors which will
I have a program that is creating an ItemizedOverlay and a map. Everything works
Synergy is a program that enables you to use the same mouse and keyboard
I'm writing a small program that prints a menu screen with a list of
Are there any O/R mappers out there that will automatically create or modify the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.