65.9K
CodeProject is changing. Read more.
Home

Count Number of Unique Colors in an Image

emptyStarIconemptyStarIconemptyStarIconemptyStarIconemptyStarIcon

0/5 (0 vote)

Feb 19, 2011

CPOL
viewsIcon

12979

I think we may check if the color is repeated or not, any how this another alternative but in VB.NetPublic Shared Function GetImageColorsCount(ByVal bitmap As Bitmap) As Integer Dim ColorList As New System.Collections.Generic.List(Of Integer) Dim clr As Integer For...

I think we may check if the color is repeated or not, any how this another alternative but in VB.Net
Public Shared Function GetImageColorsCount(ByVal bitmap As Bitmap) As Integer
        Dim ColorList As New System.Collections.Generic.List(Of Integer)
        Dim clr As Integer
        For y As Integer = 0 To bitmap.Height - 1
            For x As Integer = 0 To bitmap.Width - 1
                clr = bitmap.GetPixel(x, y).ToArgb()
                If Not ColorList.Contains(clr) Then ColorList.Add(clr)
            Next
        Next
        GetImageColorsCount = ColorList.Count
End Function