日期:2014-05-18  浏览次数:20896 次

如何遍历某个图片所有像素并找出用的像素最多的颜色
如题。。。

如何遍历某个图片所有像素并找出用的像素最多的颜色?

遍历图片像素我倒是实现了,可是找出最多的颜色这个,具体怎么做,求指教。。谢谢。。。

------解决方案--------------------
Dictionary<Color,int> mapSt=new Dictionary<Color,int>();

while(...)
{
Color crPixel;//假设这个是遍历得到的每个像素颜色值
....

if (mapSt.ContainsKey(crPixel))
{
mapSt[crPixel]=mapSt[crPixel]+1;
}
else
{
mapSt[crPixel]=1;
}


}

//mapSt里就是每个颜色值的像素数目,取出最多一个就可以
Color crMax;
int nMaxCount=-1;
bool bSet=false;
foreach(KeyValuePair<Color,int> rPair in mapSt)
{
if (!bSet||rPair.Value>nMaxCount)
{
crMax=rPair.Key;
nMaxCount=rPair.Value;
}
}