如何找到像素色彩点?

时间:2018-06-29 14:27:27

标签: c# colors

我正在尝试查找具有特定颜色的像素的坐标。

此刻,我已经获得了图像的屏幕快照,但是我不知道从这里去哪里。

现有代码:

    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;
    }

1 个答案:

答案 0 :(得分:0)

遍历:

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;
                    }
                }
            }