Search code examples
c#.netsystem.drawing

How to determine if an image contains a specific color using?


I need to determine if an image contains a specific color:

r:255
g:0
b:192

I found this, but instead of returning points, I need to return a Boolean value if the image contains the above color.

public static List<Point> FindAllPixelLocations(this Bitmap img, Color color)
        {
            var points = new List<Point>();

            int c = color.ToArgb();

            for (int x = 0; x < img.Width; x++)
            {
                for (int y = 0; y < img.Height; y++)
                {
                    if (c.Equals(img.GetPixel(x, y).ToArgb())) points.Add(new Point(x, y));
                }
            }

            return points;
        }

Solution

  • Sounds like you just need to replace

    if (c.Equals(img.GetPixel(x, y).ToArgb())) points.Add(new Point(x, y));
    

    by

    if (c.Equals(img.GetPixel(x, y).ToArgb())) return true;