Bende bir zamanlar bunu kullanmıştım, ama ağırlık olan rengi almıştım sanırım. Eski bir kod açıkcası.
public static class PictureAnalysis
{
public static List<Color> TenMostUsedColors { get; private set; }
public static List<int> TenMostUsedColorIncidences { get; private set; }
public static Color MostUsedColor { get; private set; }
public static int MostUsedColorIncidence { get; private set; }
private static int pixelColor;
private static Dictionary<int, int> dctColorIncidence;
public static void GetMostUsedColor(Bitmap theBitMap)
{
TenMostUsedColors = new List<Color>();
TenMostUsedColorIncidences = new List<int>();
MostUsedColor = Color.Empty;
MostUsedColorIncidence = 0;
// does using Dictionary<int,int> here
// really pay-off compared to using
// Dictionary<Color, int> ?
// would using a SortedDictionary be much slower, or ?
dctColorIncidence = new Dictionary<int, int>();
// this is what you want to speed up with unmanaged code
for (int row = 0; row < theBitMap.Size.Width; row++)
{
for (int col = 0; col < theBitMap.Size.Height; col++)
{
pixelColor = theBitMap.GetPixel(row, col).ToArgb();
if (dctColorIncidence.Keys.Contains(pixelColor))
{
dctColorIncidence[pixelColor]++;
}
else
{
dctColorIncidence.Add(pixelColor, 1);
}
}
}
// note that there are those who argue that a
// .NET Generic Dictionary is never guaranteed
// to be sorted by methods like this
var dctSortedByValueHighToLow = dctColorIncidence.OrderByDescending(x => x.Value).ToDictionary(x => x.Key, x => x.Value);
// this should be replaced with some elegant Linq ?
foreach (KeyValuePair<int, int> kvp in dctSortedByValueHighToLow.Take(10))
{
TenMostUsedColors.Add(Color.FromArgb(kvp.Key));
TenMostUsedColorIncidences.Add(kvp.Value);
}
MostUsedColor = Color.FromArgb(dctSortedByValueHighToLow.First().Key);
MostUsedColorIncidence = dctSortedByValueHighToLow.First().Value;
}
}
public static System.Drawing.Color GetDominantColor(Bitmap bmp)
{
if (bmp == null)
{
throw new ArgumentNullException("bmp");
}
BitmapData srcData = bmp.LockBits(new Rectangle(0, 0, bmp.Width, bmp.Height), ImageLockMode.ReadOnly, bmp.PixelFormat);
int bytesPerPixel = Image.GetPixelFormatSize(srcData.PixelFormat) / 8;
int stride = srcData.Stride;
IntPtr scan0 = srcData.Scan0;
long[] totals = new long[] { 0, 0, 0 };
int width = bmp.Width * bytesPerPixel;
int height = bmp.Height;
unsafe
{
byte* p = (byte*)(void*)scan0;
for (int y = 0; y < height; y++)
{
for (int x = 0; x < width; x += bytesPerPixel)
{
totals[0] += p[x + 0];
totals[1] += p[x + 1];
totals[2] += p[x + 2];
}
p += stride;
}
}
long pixelCount = bmp.Width * height;
int avgB = Convert.ToInt32(totals[0] / pixelCount);
int avgG = Convert.ToInt32(totals[1] / pixelCount);
int avgR = Convert.ToInt32(totals[2] / pixelCount);
bmp.UnlockBits(srcData);
return Color.FromArgb(avgR, avgG, avgB);
}