Get Palette from Bitmap - CSharp System.Drawing

CSharp examples for System.Drawing:Image Operation

Description

Get Palette from Bitmap

Demo Code


using System.Drawing.Imaging;
using System.Drawing;
using System.Text;
using System.Collections.Generic;
using System;//w  w w .  j  av a2  s  . c o m

public class Main{
        static public List<Color> GetPalette(Bitmap bitmap)
        {
            List<Color> palette = new List<Color>(0x100);

            if ((bitmap.PixelFormat & PixelFormat.Indexed) == PixelFormat.Indexed)
            {
                palette.AddRange(bitmap.Palette.Entries);
            }
            else
            {
                for (int i = 0; i < bitmap.Width; i++)
                {
                    for (int j = 0; j < bitmap.Height; j++)
                    {
                        Color color = bitmap.GetPixel(i, j);
                        if (!palette.Contains(color))
                        {
                            palette.Add(color);
                        }
                    }
                }
            }
            return palette;
        }
}

Related Tutorials