Search code examples
c#.netlistvalue-type

List returns what it does not contain too


I have an image and Im adding some of it pixels in a list given below.

 List<Color> ycolo = new List<Color>();
 for (int p = 5; p < FilteredImage.Width; p++) { 
      for (int k = 5; k < FilteredImage.Height ;k++)
      {
          ycolo.Add(FilteredImage.GetPixel(p, k));

          if (k==10) { break; }
      }
    if (p== 20) { break; }
}


if (!ycolo.Contains(FilteredImage.GetPixel(21,11)))
{
    MessageBox.Show("Im here");
}
else
{ MessageBox.Show("Im not here"); }

It returns true(Im here), thoguh it does not contain the pixle at 21,11 position What is wrong here.Im working in Visual Studio c#. What to do make it work?


Solution

  • You mixed up the position and the color of a pixel. The method FilteredImage.GetPixel(21,11) returns the color of the pixel.

    To test if a pixel at a position was added to the list use this code:

    List<System.Drawing.Point> ycolo = new List<System.Drawing.Point>();
    
    for (int p = 5; p < FilteredImage.Width; p++)
    {
        for (int k = 5; k < FilteredImage.Height; k++)
        {
            ycolo.Add(new System.Drawing.Point(p, k));
    
            if (k == 10) { break; }
        }
        if (p == 20) { break; }
    }
    
    if (ycolo.Contains(new System.Drawing.Point(21, 11)))
    {
        MessageBox.Show("Im here");
    }
    else
    {
        MessageBox.Show("Im not here");
    }