AForge.Imaging.Image.IsGrayscale C# (CSharp) Method

IsGrayscale() public static method

Check if specified 8 bpp image is grayscale.
The methods checks if the image is a grayscale image of 256 gradients. The method first examines if the image's pixel format is Format8bppIndexed and then it examines its palette to check if the image is grayscale or not.
public static IsGrayscale ( Bitmap image ) : bool
image System.Drawing.Bitmap Image to check.
return bool
        public static bool IsGrayscale( Bitmap image )
        {
            bool ret = false;

            // check pixel format
            if ( image.PixelFormat == PixelFormat.Format8bppIndexed )
            {
                ret = true;
                // check palette
                ColorPalette cp = image.Palette;
                Color c;
                // init palette
                for ( int i = 0; i < 256; i++ )
                {
                    c = cp.Entries[i];
                    if ( ( c.R != i ) || ( c.G != i ) || ( c.B != i ) )
                    {
                        ret = false;
                        break;
                    }
                }
            }
            return ret;
        }