Click here to Skip to main content
15,867,488 members
Articles / Desktop Programming / Windows Forms
Alternative
Tip/Trick

Count Number of Unique Colors in an Image

Rate me:
Please Sign up or sign in to vote.
4.92/5 (4 votes)
20 Feb 2011CPOL 15.7K   2   8
Just a few minor improvements to readability and such...The catch/throw isn't needed here, since you are just throwing it without doing anything in the catch block. A try/finally could have been used on its own.The try/finally isn't necessary because a using statement can achieve the same...
Just a few minor improvements to readability and such...

  • The catch/throw isn't needed here, since you are just throwing it without doing anything in the catch block. A try/finally could have been used on its own.
  • The try/finally isn't necessary because a using statement can achieve the same result without the extra code.

public static int CountImageColors(string fileName)
{
    HashSet<Color> colors = new HashSet<Color>();
 
    if (File.Exists(fileName))
    {
        using (Bitmap bmp = new Bitmap(fileName))
        {
            for (int x = 0; x < bmp.Width; ++x)
            {
                for (int y = 0; y < bmp.Height; ++y)
                {
                    colors.Add(bmp.GetPixel(x, y));
                }
            }
        }
    }

    return colors.Count;
}


In addition to the above, you could also use unsafe code to read out the pixels even faster, an example can be found here[^].

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Architect
United States United States
Since I've begun my profession as a software developer, I've learned one important fact - change is inevitable. Requirements change, code changes, and life changes.

So..If you're not moving forward, you're moving backwards.

Comments and Discussions

 
GeneralI would agree that readability is very important code metric... Pin
DrABELL24-Feb-11 3:39
DrABELL24-Feb-11 3:39 
GeneralHi Andrew, I have found a discussion on the net regarding nu... Pin
DrABELL23-Feb-11 9:11
DrABELL23-Feb-11 9:11 
GeneralRe: Assigning null to a variable is very inexpensive. In every ... Pin
Andrew Rissing24-Feb-11 3:16
Andrew Rissing24-Feb-11 3:16 
GeneralGood, thanks! Should you set the object: colors=null in the ... Pin
DrABELL23-Feb-11 7:08
DrABELL23-Feb-11 7:08 
GeneralRe: There would be no need. The garbage collector would take ca... Pin
Andrew Rissing23-Feb-11 8:54
Andrew Rissing23-Feb-11 8:54 
GeneralReason for my vote of 5 I like this simple, elegant solution... Pin
DrABELL22-Feb-11 16:53
DrABELL22-Feb-11 16:53 
GeneralRe: Aside from going row then column, the code is equivalent to ... Pin
Andrew Rissing23-Feb-11 3:27
Andrew Rissing23-Feb-11 3:27 
GeneralI don't normally use unsafe code in a .Net app. Pin
#realJSOP21-Feb-11 6:07
mve#realJSOP21-Feb-11 6:07 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.