title: Get the percentage usage of every colour in an image
body: <p>I have this one working but it is so damn slow on jpeg images and also needs some changing.</p>
  
  <p>I need to know the individual colours in an image (with a tolerance of +/- 1 for RGB) and the % of the image that is that colour.</p>
  
  <p>so if an image was black and white it would say something like
  White : 74%
  Black : 26%</p>
  
  <p>The code below works like I said but I need to add a tolerance system as well and I have no idea on how I would do that.</p>
  
  <pre><code>private Dictionary&lt;string, string&gt; getPixelData(Bitmap image)
  {
      Dictionary&lt;string, string&gt; pixelData = new Dictionary&lt;string, string&gt;();
      //int col, row;
      //int r, g, b;
      Color pixel;
  
      double offset = 0.000001;
      int hmm = (image.Height * image.Width);
      double current = 0;
      offset = 100 / double.Parse(hmm.ToString());// 0.01;// 100 / (image.Height * image.Width) * 10000;
  
      try
      {
          for (int i = 0; i &lt; image.Height; i++)
          {
              for (int j = 0; j &lt; image.Width; j++)
              {
                  current = current + offset;
                  pixel = image.GetPixel(i, j);                        
                  pixelData.Add(i + "," + j, (pixel.R.ToString() + " " + pixel.G.ToString() + " " + pixel.B.ToString()));
                  pBarprocess.Value = int.Parse(Math.Floor(current).ToString());
                  pBarprocess.Update();
                  Application.DoEvents();
              }
          }
      }
      catch (Exception ex)
      {
          MessageBox.Show("Unable to parse image " + ex);
      }
  
      return pixelData;
  }
  </code></pre>
  
  <p>And the other function</p>
  
  <pre><code>private void btnProcess_Click(object sender, EventArgs e)
  {
      pBarprocess.Value = 0;
      pBarprocess.Enabled = false;
      Bitmap foo = Bitmap.FromFile(@txtFileName.Text) as Bitmap;
      Dictionary&lt;string, string&gt; pixelData = new Dictionary&lt;string, string&gt;();
  
      lblProcess.Text = "Processing pixel map";
      pixelData = getPixelData(foo);
  
      lblProcess.Text = "Calculating Density";
      lblProcess.Update();
  
      var distinctList = pixelData.Values.Distinct().ToList();
  
      Console.WriteLine("DL = " + distinctList.Count);
      double offset = 100 / double.Parse(distinctList.Count.ToString());
      double current = 0;
  
      foreach (var value in distinctList)
      {
          IEnumerable&lt;string&gt; query = pixelData.Values.Where(fruit =&gt; fruit == value);
          double perc = (double.Parse(query.Count().ToString()) / double.Parse(pixelData.Count.ToString())) * 100;
          Console.WriteLine(value + " = " + query.Count() + "(" + perc + "%)");
          txtAnalysis.Text = "Colour " + value + " : " + query.Count() + " (" + perc.ToString() + "%)\r\n" + txtAnalysis.Text;
          txtAnalysis.Update();
          pBarprocess.Value = int.Parse(Math.Floor(current).ToString());
          pBarprocess.Update();
          Application.DoEvents();
      }
  
      lblProcess.Text = "Finished.";
      pBarprocess.Value = 0;
      pBarprocess.Enabled = false;
  }
  </code></pre>