Click here to Skip to main content
11,706,979 members (52,639 online)
Click here to Skip to main content

Tagged as

Count Number of Unique Colors in an Image

, 20 Feb 2011 CPOL 5K 1
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.

You may also be interested in...

Comments and Discussions

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

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.150819.1 | Last Updated 20 Feb 2011
Article Copyright 2011 by Andrew Rissing
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid