从图像中获取颜色方案

Mik*_*e B 9 c# color-scheme

我想开发一个像这里的特色基本工具.我将截取一些网页的截图,并从那里我想采取前五种最流行的颜色,并从那里以某种方式决定颜色是否是一个很好的匹配.

我想用C#编写这个工具,经过一些研究后我发现了lockbits.我的第一个想法是拍摄一张图像然后获得每个像素的颜色,但我不确定这是否会给我我想要的结果以及如何制作六种最流行的颜色列表.

这里的任何人都可以提供关于如何创建程序以执行与上述程序类似的操作的建议,该程序将采用图像并选择图像中使用的前五种颜色吗?

ser*_*hio 16

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

更新的代码:

    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; }
Run Code Online (Sandbox Code Playgroud)

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

  • 那就是天才!我从来没有想过这样做.我还不能测试代码,因为我的笔记本电脑坏了,但我会接受任何答案.非常感谢你! (2认同)