c#比较特定的像素颜色与图片颜色

时间:2020-06-26 14:35:33

标签: c#

我想实时将屏幕中间像素的颜色与图片的颜色进行比较,并在它们重叠时开始某种动作。

问题是我要与之进行颜色比较的图片有太多不同的颜色和细微差别,从而为它们赋予了不同的颜色代码。

结果,如果不列出数百或数千种颜色进行比较(几乎会使代码变慢),几乎不可能获得准确一致的结果。

这是我到目前为止所获得的,我不知道如何解决这个基本问题。其余代码可能也很糟糕,很抱歉;我真的是编程新手。谢谢您的回答!

static void Main(string[] args)
{
    while (true)
    {
        List<string> givenList = new List<string> { "#FEFEFE", "#000000" };
        //insert as many colors as you want, here white and black

        Bitmap bitmap = new Bitmap(SystemInformation.VirtualScreen.Width, SystemInformation.VirtualScreen.Height); 
        Graphics graphics = Graphics.FromImage(bitmap as Image);
        graphics.CopyFromScreen(0, 0, 0, 0, bitmap.Size);
        Color middleCol = bitmap.GetPixel(SystemInformation.VirtualScreen.Width / 2, SystemInformation.VirtualScreen.Height / 2); //get the color value of only the middle Pixel and turn it into a string, so it can be compared to the list
        string currentColor = ColorTranslator.ToHtml(middleCol);
        Console.WriteLine("Current:" + currentColor);
        Thread.Sleep(50);
        foreach (string s in givenList) //do a certain action if the values overlap
        {
            if(s == currentColor)
            {
                //do whatever
            }
        }
    }
}

0 个答案:

没有答案
相关问题