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

相关文章

1:最直白的循环遍历方法,可以分为遍历key--value键值对以及...
private void ClearTextBox(){ foreach (var control in pnl...
原文叫看《墨攻》理解IOC概念 2006年多部贺岁大片以让人应接...
右击文件夹-&gt;安全选项卡-&gt;添加-&gt;高级-...