Get Color Palette - CSharp System.Drawing

CSharp examples for System.Drawing:Color

Description

Get Color Palette

Demo Code


using System.Drawing;
using System.Drawing.Imaging;
using System;/* w  w w .  j  a v a 2 s .c om*/

public class Main{
    public static ColorPalette GetColorPalette(int nColors)
      {
         // Assume monochrome image.
         PixelFormat bitscolordepth = PixelFormat.Format1bppIndexed;
         ColorPalette palette;    // The Palette we are stealing
         Bitmap bitmap;     // The source of the stolen palette

         // Determine number of colors.
         if (nColors > 2) bitscolordepth = PixelFormat.Format4bppIndexed;
         if (nColors > 16) bitscolordepth = PixelFormat.Format8bppIndexed;

         // Make a new Bitmap object to get its Palette.
         bitmap = new Bitmap(1, 1, bitscolordepth);

         palette = bitmap.Palette;   // Grab the palette

         bitmap.Dispose();           // cleanup the source Bitmap

         return palette;             // Send the palette back
      }
}

Related Tutorials