1. 程式人生 > >判斷一張圖片的顏色數和亮度值

判斷一張圖片的顏色數和亮度值

stat args CP eight turn bit direct 路徑 name

 static void Main(string[] args)
        {
           var di=new DirectoryInfo(@"C:\Users\Administrator\Desktop\pic");
            foreach (var fi in di.GetFiles())
            {
                var colorCount = 0;
                float lightness = 0;
                GetPicColorCountAndLightness(fi.FullName, 
out colorCount, out lightness); Console.WriteLine(fi.Name + " " + colorCount + " " + lightness); } Console.ReadKey(); } /// <summary> /// 功能:輸入一個圖片的路徑,返回這張圖片使用的顏色個數和亮度 /// 作者:黃海 /// 時間:2018-06-12 /// </summary>
/// <param name="picPath"></param> /// <param name="colorCount"></param> /// <param name="lightNess"></param> /// <returns></returns> public static void GetPicColorCountAndLightness(string picPath,out int colorCount,out float
lightNess) { var dict=new Dictionary<Color,int>(); var bmpobj = new Bitmap(picPath); float allLightness = 0; int count = 0; for (var i = 0; i < bmpobj.Height; i++) { for (var j = 0; j < bmpobj.Width; j++) { count++; var a=bmpobj.GetPixel(j, i); dict[a] = 1; //float hue = a.GetHue(); //float saturation = a.GetSaturation(); allLightness += a.GetBrightness(); } } colorCount = dict.Count; lightNess = allLightness; }

判斷一張圖片的顏色數和亮度值