简体   繁体   中英

How can I find Point of a Pixel Colour?

I am trying to find the coordinates of the pixel with a specific colour.

At the moment I have got a screen shot of the image, but I don't know where to go from here.

Existing code:

    public Form1()
    {
        InitializeComponent();
    }

    private Bitmap Img;

    private void button2_Click(object sender, EventArgs e)
    {
        var bmpScreenshot = new Bitmap(Screen.PrimaryScreen.Bounds.Width,
            Screen.PrimaryScreen.Bounds.Height,
            PixelFormat.Format32bppArgb);

        // Create a graphics object from the bitmap.
        var gfxScreenshot = Graphics.FromImage(bmpScreenshot);

        // Take the screenshot from the upper left corner to the right bottom corner.
        gfxScreenshot.CopyFromScreen(Screen.PrimaryScreen.Bounds.X,
            Screen.PrimaryScreen.Bounds.Y,
            0,
            0,
            Screen.PrimaryScreen.Bounds.Size,
            CopyPixelOperation.SourceCopy);

        // Save the screenshot to the specified path that the user has chosen.
        bmpScreenshot.Save("Screenshot.png", ImageFormat.Png);
    }

    //Want to get the location of the colour: #AA00FF
    private Point GetLocation()
    {
        Img = new Bitmap("Screenshot.png");

        Point p = new Point(1, 1);
        //This should read:
        //p = Img.GetLocationOfPixel('FF00AA');

        return p;
    }

Looping through:

private Point GetLocation()
        {
            Img = new Bitmap("Screenshot.png");
            Color pixelColor;

            for (int y = 0; y < Img.Height; y++)
            {
                for (int x = 0; x < Img.Width; x++)
                {
                    pixelColor = Img.GetPixel(x, y);
                    if (pixelColor.R == 84 && pixelColor.G == 96 && pixelColor.B == 103)
                    {
                        MessageBox.Show($"Location is Y:{y.ToString()} X:{x.ToString()}");
                        Point p = new Point(x, y);
                        return p;
                    }
                }
            }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM