CSharp examples for System.Drawing:Bitmap
Counts the number of colors in a bitmap, up through the given maximum. If the maximum is reached, the method will return early.
using System.Windows.Forms; using System.Text; using System.Linq; using System.Drawing; using System.Collections.Generic; using System;// w w w. j a v a 2s . co m public class Main{ /// <summary> /// Counts the number of colors in a bitmap, up through the given maximum. If the maximum is reached, the method will return early. /// </summary> public static int CountColors(Bitmap bmp, int max) { int count = 0; HashSet<Color> colors = new HashSet<Color>(); for (int y = 0; y < bmp.Height && count < max; y++) { for (int x = 0; x < bmp.Width && count < max; x++) { Color color = bmp.GetPixel(x, y); if (!colors.Contains(color)) { colors.Add(color); count++; } } } return count; } }