Posted By

vcskicks on 03/21/09


Tagged


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

umang_nine


Extract Dominant Image Color


 / Published in: C#
 

URL: http://www.vcskicks.com/dominant-color.php

  1. public static Color getDominantColor(Bitmap bmp)
  2. {
  3. //Used for tally
  4. int r = 0;
  5. int g = 0;
  6. int b = 0;
  7.  
  8. int total = 0;
  9.  
  10. for (int x = 0; x < bmp.Width; x++)
  11. {
  12. for (int y = 0; y < bmp.Height; y++)
  13. {
  14. Color clr = bmp.GetPixel(x, y);
  15.  
  16. r += clr.R;
  17. g += clr.G;
  18. b += clr.B;
  19.  
  20. total++;
  21. }
  22. }
  23.  
  24. //Calculate average
  25. r /= total;
  26. g /= total;
  27. b /= total;
  28.  
  29. return Color.FromArgb(r, g, b);
  30. }

Report this snippet  

You need to login to post a comment.