加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – 从图像中获取颜色方案

发布时间:2020-12-15 04:09:30 所属栏目:百科 来源:网络整理
导读:我想开发一个基本的工具,像一个 featured here.我将截取一些网页的屏幕截图,从那里我希望采取排名前五的最流行的颜色,从那里以某种方式决定颜色是否是一个很好的匹配. 我想在C#中编写这个工具,经过一番研究,我发现了锁.我的第一个想法是拍摄图像,然后获取每
我想开发一个基本的工具,像一个 featured here.我将截取一些网页的屏幕截图,从那里我希望采取排名前五的最流行的颜色,从那里以某种方式决定颜色是否是一个很好的匹配.

我想在C#中编写这个工具,经过一番研究,我发现了锁.我的第一个想法是拍摄图像,然后获取每个像素的颜色,但我不确定这是否会给我我想要的结果,以及如何制作六个最受欢迎的颜色列表.

任何人都可以提供建议,我将如何创建一个类似于上述程序的程序,这将采取一个图像,并将选择图像中使用的前五种颜色?

解决方法

嗯..使用缩略图(16×16,32×32等),并从中选择颜色

更新代码:

private void button1_Click(object sender,EventArgs e)
    {
        int thumbSize = 32;
        Dictionary<Color,int> colors = new Dictionary<Color,int>();

        Bitmap thumbBmp = 
            new Bitmap(pictureBox1.BackgroundImage.GetThumbnailImage(
                thumbSize,thumbSize,ThumbnailCallback,IntPtr.Zero));

        //just for test
        pictureBox2.Image = thumbBmp;            

        for (int i = 0; i < thumbSize; i++)
        {
            for (int j = 0; j < thumbSize; j++)
            {
                Color col = thumbBmp.GetPixel(i,j);
                if (colors.ContainsKey(col))
                    colors[col]++;
                else
                    colors.Add(col,1);
            }                
        }

        List<KeyValuePair<Color,int>> keyValueList = 
            new List<KeyValuePair<Color,int>>(colors);

        keyValueList.Sort(
            delegate(KeyValuePair<Color,int> firstPair,KeyValuePair<Color,int> nextPair)
            {
                return nextPair.Value.CompareTo(firstPair.Value);
            });

        string top10Colors = "";
        for (int i = 0; i < 10; i++)
        {
            top10Colors += string.Format("n {0}. {1} > {2}",i,keyValueList[i].Key.ToString(),keyValueList[i].Value);
            flowLayoutPanel1.Controls[i].BackColor = keyValueList[i].Key;
        }
        MessageBox.Show("Top 10 Colors: " + top10Colors);
    }

    public bool ThumbnailCallback() { return false; }

alt text http://lh3.ggpht.com/_1TPOP7DzY1E/S0uZ6GGD4oI/AAAAAAAAC5k/3Psp1cOCELY/s800/colors.png

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读