Extract Dominant Image Color


/ Published in: C#
Save to your folder(s)



Copy this code and paste it in your HTML
  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. }

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

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.