Search code examples
c#color-detection

C# Color Detection more than one result


i've a question about color detection. i've code but i want more than one result. The code is famous but I want to the program to bring me all the results not just one result. I hope I made myself clear.

My bad I made the missing copy

private Boolean FindBitmap(Bitmap bmpNeedle, Bitmap bmpHaystack, out Point location)
        {
            try
            {

                for (int outerX = 0; outerX < bmpHaystack.Width - bmpNeedle.Width; outerX++)
                {
                    for (int outerY = 0; outerY < bmpHaystack.Height - bmpNeedle.Height; outerY++)
                    {
                        for (int innerX = 0; innerX < bmpNeedle.Width; innerX++)
                        {
                            for (int innerY = 0; innerY < bmpNeedle.Height; innerY++)
                            {
                                Color cNeedle = bmpNeedle.GetPixel(innerX, innerY);
                                Color cHaystack = bmpHaystack.GetPixel(innerX + outerX, innerY + outerY);

                                if (cNeedle.R != cHaystack.R || cNeedle.G != cHaystack.G || cNeedle.B != cHaystack.B)
                                {
                                    goto notFound;
                                }
                            }
                        }
                        location = new Point(outerX, outerY);
                        listBox1.Items.Add(location);
                        MessageBox.Show(location.ToString());
                        notFound:
                        continue;
                    }
                }

            }
            catch (Exception)
            {

            }
            location = Point.Empty;
            return false;
        }

Solution

  • I want to the program to bring me all the results not just one result

    Return a List of all results. So you would modify your method to have a different return type:

    public List<Point> FindBitmap(Bitmap bmpNeedle, Bitmap bmpHaystack, out Point location)
    {
        List<Point> results = new List<Point>();
    
        for (int outerX = 0; outerX < bmpHaystack.Width - bmpNeedle.Width; outerX++)
        {
            for (int outerY = 0; outerY < bmpHaystack.Height - bmpNeedle.Height; outerY++)
            {
                for (int innerX = 0; innerX < bmpNeedle.Width; innerX++)
                {
                    for (int innerY = 0; innerY < bmpNeedle.Height; innerY++)
                    {
                        Color cNeedle = bmpNeedle.GetPixel(innerX, innerY);
                        Color cHaystack = bmpHaystack.GetPixel(innerX + outerX, innerY + outerY);
                        if (cNeedle.R != cHaystack.R || cNeedle.G != cHaystack.G || cNeedle.B != cHaystack.B)
                        {
                            goto notFound;
                        }
                    }
                }
                location = new Point(outerX, outerY);
                // collect the result
                results.Add(location);
                notFound:
                continue;
            }
        }
    
        // when you are finished looping return it
        return results;
    
    }