如题如何遍历某个图片所有像素并找出用的像素最多的颜色?遍历图片像素我倒是实现了,可是找出最多的颜色这个,具体怎么做,求指教。。谢谢

解决方案 »

  1.   

    用Dictionary统计颜色的像素数目,key是RGB值,value是颜色等于key的像素的数目,遍历的时候统计,
      

  2.   

    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;
       }
    }