【发布时间】:2014-12-20 21:18:25
【问题描述】:
我需要图像中最常用的 30 种颜色列表...我明白了,但是有很多色调,我无法获得主要颜色。例如图片有红色,但我不能得到红色。
我能做什么?
Dictionary<string,int> d = new Dictionary<string, int>();
Bitmap bmp = (Bitmap)Bitmap.FromFile("c:\\test.jpg");
for (int x = 0; x < bmp.Width; x++)
{
for (int y = 0; y < bmp.Height; y++)
{
Color c = bmp.GetPixel(x, y);
var hexColor = c.R.ToString("X") + c.G.ToString("X") + c.B.ToString("X");
if (d.ContainsKey(hexColor))
{
d[hexColor] = ++d[hexColor];
}
else
{
d.Add(hexColor, 1);
}
}
}
var items = (from pair in d
orderby pair.Value descending
select pair);
System.IO.StreamWriter file =
new System.IO.StreamWriter("c:\\colors.html",false);
foreach (var v in items)
{
file.WriteLine("<div style='width:50px;height:50px;float:left;margin-right:10px;background-color:#" + v.Key + "'> </div>");
}
file.Close();
【问题讨论】: