Click here to Skip to main content
Click here to Skip to main content

Count Number of Unique Colors in an Image

By , 20 Feb 2011
 
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)

About the Author

Andrew Rissing
Software Developer (Senior)
United States United States
Member
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.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
Hint: For improved responsiveness ensure Javascript is enabled and choose 'Normal' from the Layout dropdown and hit 'Update'.
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
GeneralI would agree that readability is very important code metric...memberDrABELL24 Feb '11 - 3:39 
GeneralHi Andrew, I have found a discussion on the net regarding nu...memberDrABELL23 Feb '11 - 9:11 
GeneralRe: Assigning null to a variable is very inexpensive. In every ...memberAndrew Rissing24 Feb '11 - 3:16 
GeneralGood, thanks! Should you set the object: colors=null in the ...memberDrABELL23 Feb '11 - 7:08 
GeneralRe: There would be no need. The garbage collector would take ca...memberAndrew Rissing23 Feb '11 - 8:54 
GeneralReason for my vote of 5 I like this simple, elegant solution...memberDrABELL22 Feb '11 - 16:53 
GeneralRe: Aside from going row then column, the code is equivalent to ...memberAndrew Rissing23 Feb '11 - 3:27 
GeneralI don't normally use unsafe code in a .Net app.mvpJohn Simmons / outlaw programmer21 Feb '11 - 6:07 

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

Permalink | Advertise | Privacy | Mobile
Web01 | 2.6.130516.1 | Last Updated 20 Feb 2011
Article Copyright 2011 by Andrew Rissing
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid