Click here to Skip to main content
Click here to Skip to main content
Alternative Tip/Trick

Tagged as

Count Number of Unique Colors in an Image

, 20 Feb 2011 CPOL
Rate this:
Please Sign up or sign in to vote.
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)

Share

About the Author

Andrew Rissing
Software Developer (Senior)
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... PinmemberDrABELL24-Feb-11 4:39 
GeneralHi Andrew, I have found a discussion on the net regarding nu... PinmemberDrABELL23-Feb-11 10:11 
GeneralRe: Assigning null to a variable is very inexpensive. In every ... PinmemberAndrew Rissing24-Feb-11 4:16 
GeneralGood, thanks! Should you set the object: colors=null in the ... PinmemberDrABELL23-Feb-11 8:08 
GeneralRe: There would be no need. The garbage collector would take ca... PinmemberAndrew Rissing23-Feb-11 9:54 
GeneralReason for my vote of 5 I like this simple, elegant solution... PinmemberDrABELL22-Feb-11 17:53 
GeneralRe: Aside from going row then column, the code is equivalent to ... PinmemberAndrew Rissing23-Feb-11 4:27 
GeneralI don't normally use unsafe code in a .Net app. PinmvpJohn Simmons / outlaw programmer21-Feb-11 7:07 

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

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

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