TizzyT

ImageColorCounter -TizzyT

Dec 23rd, 2015
1,372
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VB.NET 2.17 KB | None | 0 0
  1. Imports System.Drawing
  2. Module ImageColorCounter
  3.     Sub Main()
  4.         If My.Application.CommandLineArgs.Count > 0 Then
  5.             Dim img As Bitmap
  6.             Try
  7.                 img = New Bitmap(My.Application.CommandLineArgs(0))
  8.             Catch ex As Exception
  9.                 Console.WriteLine("Failed to load Image:" & vbCrLf & My.Application.CommandLineArgs(0))
  10.                 Console.WriteLine("Press Enter")
  11.                 Console.ReadLine()
  12.                 Exit Sub
  13.             End Try
  14.             Dim ColorDictionary As New Dictionary(Of Color, Integer)
  15.             For y = 0 To img.Height - 1
  16.                 For x = 0 To img.Width - 1
  17.                     Dim C As Color = img.GetPixel(x, y)
  18.                     If ColorDictionary.ContainsKey(C) Then ColorDictionary(C) += 1 Else ColorDictionary.Add(C, 1)
  19.                 Next
  20.             Next
  21.             Console.WriteLine("There are " & ColorDictionary.Count & " colors in this image")
  22.             Console.WriteLine("Press C to cancel, Any key to see color table")
  23.             If Console.ReadKey.Key = ConsoleKey.C Then
  24.                 Exit Sub
  25.             Else
  26.                 Dim I As Integer = 0
  27.                 Dim count As Integer = 0
  28.                 For Each C As KeyValuePair(Of Color, Integer) In ColorDictionary
  29.                     I += 1
  30.                     Console.WriteLine(I & vbTab & "[A:" & C.Key.A & vbTab & _
  31.                                       "R:" & C.Key.R & vbTab & _
  32.                                       "G:" & C.Key.G & vbTab & _
  33.                                       "B:" & C.Key.B & vbTab & "] = " & _
  34.                                       C.Value)
  35.                     count += 1
  36.                     If count = 256 AndAlso I < ColorDictionary.Count Then
  37.                         count = 0
  38.                         Console.WriteLine("Press C to cancel, Any key to see color table")
  39.                         If Console.ReadKey.Key = ConsoleKey.C Then Exit Sub
  40.                     End If
  41.                 Next
  42.                 Console.WriteLine("Color table completed, Press any key to exit")
  43.                 Console.ReadKey()
  44.             End If
  45.         End If
  46.     End Sub
  47. End Module
Advertisement
Add Comment
Please, Sign In to add comment