C# Code Snippet to return array of colors used in a bitmap

Posted on 2011-10-11
Last Modified: 2012-08-13
Any idea how i can determine a discreet list of colors that are used in a bitmap.  For example, if a bitmap contained a primitive American Flag, the method would return Color.Red, Color.White, Color.Blue.   I intend to have the return values sorted by usage within the bitmap.  So the return would be red, white then blue.

I want to write a method in C# which has the following signature?

using System;
using System.Drawing;

public static Color[] GetColorsFromBitmap(Bitmap bitmap)
     // What goes here?  How do i loop through the pixels (assuming i have to)?

Question by:RXGeorge
    LVL 85

    Accepted Solution

    Create a class to hold each color along with its count:

        public class ColorData
            public Color color;
            public int count;

    Then the code might look something like:  
    public static Color[] GetColorsFromBitmap(Bitmap bitmap)
                Dictionary<Color, ColorData> data = new Dictionary<Color, ColorData>();
                for (int x = 0; x < bitmap.Width; x++)
                    for (int y = 0; y < bitmap.Height; y++)
                        if (data.ContainsKey(bitmap.GetPixel(x,y)))
                            data[bitmap.GetPixel(x, y)].count++;
                            ColorData cd = new ColorData();
                            cd.color = bitmap.GetPixel(x, y);
                            cd.count = 1;
                            data.Add(cd.color, cd);
                List<ColorData> sortedData = data.Values.ToList();
                sortedData.Sort( delegate(ColorData cd1, ColorData cd2) { return cd2.count.CompareTo(cd1.count); } );
                List<Color> colors = new List<Color>();
                foreach (ColorData cd in sortedData)
                return colors.ToArray();

    Open in new window


    Author Closing Comment

    Fantastic,  thanks so much.

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Join & Write a Comment

    Bit flags and bit flag manipulation is perhaps one of the most underrated strategies in programming, likely because most programmers developing in high-level languages rely too much on the high-level features, and forget about the low-level ones. Th…
    Introduction                                                 Was the var keyword really only brought out to shorten your syntax? Or have the VB language guys got their way in C#? What type of variable is it? All will be revealed.   Also called…
    Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
    Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

    754 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    22 Experts available now in Live!

    Get 1:1 Help Now