Color Detecting in an Image in C#

In this "Color Detector" application, first we select an image (jpeg, bmp, png etc.) and then we pick the color which we want to detect on that image. If we found the color, we display a confirmation message.

The snapshot for the running application is below:

Color-Detector-application.gif

Now we go into more detail of this application.

Source of the image: We can take an image from any source (like Digital Camera, any stored image in PC, Mobile etc.), and keep the image somewhere in our system. The image can be in any format (i.e. jpeg, bmp, png etc.).

Image Selection: For image file selection, we include an "OpenFileDialog" control in our form. On "Choose Image" button click, we show "OpenFileDialog" to choose any image file from the system. After image file selection, we get the file path and then load the image into a picture box. For the file selection and loading into a picture box, refer to the following code:

private void ChooseImageBtn_Click(object sender, EventArgs e)
{
    try
    {
         //Clearing previously selected image from picture box
         ImagePathLbl.Text = "";
         pictureBox1.Image = null;
 
         //Showing the File Chooser Dialog Box for Image File selection
         DialogResult IsFileChosen = openFileDialog1.ShowDialog();
 
         if (IsFileChosen == System.Windows.Forms.DialogResult.OK)
         {
             //Get the File name
             ImagePathLbl.Text = openFileDialog1.FileName;
 
             //Load the image into a picture box
             if (openFileDialog1.ValidateNames == true)
             {
                  pictureBox1.Image = Image.FromFile(ImagePathLbl.Text);
             }
         }
    }
    catch (Exception exc)
    {
         MessageBox.Show(exc.Message);
    }
}

Color Selection: For detecting the selected color in our selected image, we include a "ColorDialog" control in our form. On the "Pick Color" button click, we show "ColorDialog" to choose any color from the dialog box. After color selection, we get the color name and then display the chosen color into a panel by setting the Panel.BackColor property to the chosen color. If the chosen color is a known color then we also display the color name in text. For the color selection and displaying the selected color, refer to the following code:

private void PickColorBtn_Click(object sender, EventArgs e)
{
    try
    {
        // Clean previously selected color
        SelectedColorNameLbl.Text = "";
        panelSelectedColor.BackColor = actualColor;
 
        //Showing color choice
        DialogResult IsColorChosen = colorDialog1.ShowDialog();
 
        if (IsColorChosen == System.Windows.Forms.DialogResult.OK)
        {
            panelSelectedColor.BackColor = colorDialog1.Color;
 
            //If it is know color, display the color name  
            if (colorDialog1.Color.IsKnownColor == true)
            {
                SelectedColorNameLbl.Text = colorDialog1.Color.ToKnownColor().ToString();
            }
        }
    }
    catch (Exception exc)
    {
        MessageBox.Show(exc.Message);
    }
}

Color Detection: For color detection, first we convert the loaded image into a picture box of a "BitMap" class. Refer to the following MSDN link for more details:

http://msdn.microsoft.com/en-us/library/system.drawing.bitmap.aspx

Then we iterate to each pixel of the image and get the pixel color. We store each pixel color into a temporary variable named "now_color". Get the 32-bit ARGB value of this "now_color" (a Color Object) with 32-bit ARGB value of our "Picked Color". If both values are the same, we display a "Color Found!" message and break from the iteration, otherwise we keep looking from the next pixel until we have covered the entire image. If we don't get the color we are looking for then we show a "Selected Color Not Found" Message. For the color detection, refer to the following code:

private void DetectColorBtn_Click(object sender, EventArgs e)
{
    try
    {
        Boolean IsColorFound = false;
            if (pictureBox1.Image != null)
        {
             //Converting loaded image into bitmap
             Bitmap bmp = new Bitmap(pictureBox1.Image);
 
             //Iterate whole bitmap to findout the picked color
             for (int i = 0; i < pictureBox1.Image.Height; i++)
             {
                  for (int j = 0; j < pictureBox1.Image.Width; j++)
                  {
                       //Get the color at each pixel
                       Color now_color = bmp.GetPixel(j, i);
 
                       //Compare Pixel's Color ARGB property with the picked color's ARGB property 
                       if (now_color.ToArgb() == colorDialog1.Color.ToArgb())
                       {
                            IsColorFound = true;
                            MessageBox.Show("Color Found!");
                            break;
                       }
                  }
             if (IsColorFound == true)
             {
                  break;
             }
        }
        if (IsColorFound == false)
        {
              MessageBox.Show("Selected Color Not Found.");
        }
   }
   else
   {
         MessageBox.Show("Image is not loaded");
   }
   }
   catch (Exception exc)
   {
        MessageBox.Show(exc.Message);
    }
}

Thanks


Similar Articles