Get Average Color from Bitmap - CSharp System.Drawing

CSharp examples for System.Drawing:Bitmap

Description

Get Average Color from Bitmap

Demo Code


using System.Drawing.Imaging;
using System.Drawing;
using System;// w w w  .  jav a 2s .co  m

public class Main{
        public static Color GetAverageColor(this Bitmap bmp)
        {
            if (bmp == null) { return Color.Black; }

            const int minDiversion = 15; // drop pixels that do not differ by at least minDiversion between color values (white, gray or black)
            int dropped = 0; // keep track of dropped pixels

            int width = bmp.Width;
            int height = bmp.Height;

            int red = 0;
            int green = 0;
            int blue = 0;
            
            long[] totals = { 0, 0, 0 };

            // Will fail on anything other than 24/32 bit images
            int bppModifier = bmp.PixelFormat == System.Drawing.Imaging.PixelFormat.Format24bppRgb ? 3 : 4; 

            BitmapData srcData = bmp.LockBits(new System.Drawing.Rectangle(0, 0, bmp.Width, bmp.Height), ImageLockMode.ReadOnly, bmp.PixelFormat);
            int stride = srcData.Stride;
            IntPtr Scan0 = srcData.Scan0;

            unsafe
            {
                byte* p = (byte*)(void*)Scan0;

                for (int y = 0; y < height; y++)
                {
                    for (int x = 0; x < width; x++)
                    {
                        int idx = (y * stride) + x * bppModifier;
                        red = p[idx + 2];
                        green = p[idx + 1];
                        blue = p[idx];
                        if (Math.Abs(red - green) > minDiversion || Math.Abs(red - blue) > minDiversion || Math.Abs(green - blue) > minDiversion)
                        {
                            totals[2] += red;
                            totals[1] += green;
                            totals[0] += blue;
                        }
                        else
                        {
                            dropped++;
                        }
                    }
                }
            }

            int count = width * height - dropped;
            if (count == 0) { return Color.Black; }

            int avgR = (int)(totals[2] / count);
            int avgG = (int)(totals[1] / count);
            int avgB = (int)(totals[0] / count);

            return Color.FromArgb(avgR, avgG, avgB);
        }
}

Related Tutorials