为了账号安全,请及时绑定邮箱和手机立即绑定

c# 如何获取点阵图颜色

c# 如何获取点阵图颜色

C#
慕森卡 2018-12-25 14:08:59
不是像photoshop中的滴管那样获取颜色,而是批量从一组图片里获取各图片的主要颜色,比如博客园的图片,那么将获取蓝色,红色,灰色。
查看完整描述

2 回答

?
噜噜哒

TA贡献1784条经验 获得超7个赞

从位图中将所有像素的颜色统计出来,然后根据各种颜色出现的次数做个排序。

 

        class PixelColor : IComparable<PixelColor>
        {
            private System.Drawing.Color _Color;
            private int _Count;

            public System.Drawing.Color Color
            {
                get
                {
                    return _Color;
                }

                set
                {
                    _Color = value;
                }
            }

            public int Count
            {
                get
                {
                    return _Count;
                }

                set
                {
                    _Count = value;
                }
            }

            public PixelColor(System.Drawing.Color color)
            {
                Color = color;
            }

            static public PixelColor operator ++(PixelColor pixelColor)
            {
                if (pixelColor != null)
                {
                    pixelColor.Count++;
                    return pixelColor;
                }
                else
                {
                    return null;
                }
            }

            public override string ToString()
            {
                return string.Format("Color={0} Count={1}", Color, Count);
            } 


            #region IComparable<PixelColor> Members

            public int CompareTo(PixelColor other)
            {
                return other.Count.CompareTo(this.Count);
            }

            #endregion
        }

        static private List<PixelColor> GetPixelColorFromImageFile(string fileName)
        {
            System.Drawing.Image image = System.Drawing.Image.FromFile(fileName);
            System.Drawing.Bitmap bitmap = new System.Drawing.Bitmap(image);

            Dictionary<System.Drawing.Color, PixelColor> _ColorDict = new Dictionary<System.Drawing.Color, PixelColor>();
            
            for(int x = 0; x < bitmap.Width; x++)
            {
                for(int y = 0; y < bitmap.Height; y++)
                {
                    System.Drawing.Color color = bitmap.GetPixel(x, y);

                    if (!_ColorDict.ContainsKey(color))
                    {
                        _ColorDict.Add(color, new PixelColor(color));
                    }

                    _ColorDict[color]++;
                }
            }

            List<PixelColor> pixelColorList = new List<PixelColor>(); 
            
            foreach (PixelColor pixelColor in _ColorDict.Values)
            {
                pixelColorList.Add(pixelColor);
            }

            pixelColorList.Sort();

            return pixelColorList;
        }

 

调用代码

            List<PixelColor> pixelColorList = GetPixelColorFromImageFile(@"d:\test\logo.gif");

            
            for(int i = 0 ; i < 10; i++)
            {
                Console.WriteLine(pixelColorList[i]);
            }


查看完整回答
反对 回复 2018-12-29
?
狐的传说

TA贡献1804条经验 获得超3个赞

eaglet很牛啊,不过偶还没接触过这些,只简单用过discuz的颜色拾取器代码

TimChiang不妨去试试,看是怎么实现的.

看源代码控件


查看完整回答
反对 回复 2018-12-29
  • 2 回答
  • 0 关注
  • 982 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信