Skip to content

Instantly share code, notes, and snippets.

@lukemeyer
Created September 13, 2012 10:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lukemeyer/3713450 to your computer and use it in GitHub Desktop.
Save lukemeyer/3713450 to your computer and use it in GitHub Desktop.
Calculate average Color in a bitmap
public static Color getAverageColor(Bitmap bmp)
{
//Used for tally
int r = 0;
int g = 0;
int b = 0;
int total = 0;
for (int x = 0; x < bmp.Width; x++)
{
for (int y = 0; y < bmp.Height; y++)
{
Color clr = bmp.GetPixel(x, y);
r += clr.R;
g += clr.G;
b += clr.B;
total++;
}
}
//Calculate average
r /= total;
g /= total;
b /= total;
return Color.FromArgb(r, g, b);
}
@batman
Copy link

batman commented Jan 15, 2019

Thank you, works for me

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment