0

我试图制作一种返回特定像素颜色值的方法。我遇到的问题是同一段代码在不同的显示器上返回不同的 ARGB。

目前我正在使用这种方法:

    public static Color GetColorFromPosition(string process, int x, int y)
    {
        //using bitmap object to do actions
        using (Bitmap Capture = CaptureApplication(Path.GetFileNameWithoutExtension(process)))
        {
            //copy specific part of screen and push it into bmp
            Color result = Capture.Clone(new Rectangle(x, y, 1, 1), Capture.PixelFormat).GetPixel(0,0);

            return result;
        }
    }

captureapplication 方法如下所示(该方法对打开的应用程序进行快照):

    public static Bitmap CaptureApplication(string procName)
    {
        Rectangle clientArea = GetProcessWindowRect(procName);

        Bitmap bp = new Bitmap(clientArea.Width, clientArea.Height, PixelFormat.Format32bppArgb);
        Graphics g = Graphics.FromImage(bp);
        g.CopyFromScreen(clientArea.Left, clientArea.Top, 0, 0, Screen.PrimaryScreen.Bounds.Size, CopyPixelOperation.SourceCopy);

        return bp;
    }

这将捕获一个打开的应用程序窗口,然后使用 getpixel 获取给定像素的 ARGB 值。所以举个例子,我想要 x=100 和 y=100 的颜色。在一台计算机上它会返回 [A=255, R=6, G=158, B=12] 而在另一台计算机上我得到 [A=255, R=6, G=157, B=13]。结果略有不同。

我的问题是为什么会发生这种情况并且有解决方案吗?我也尝试过使用 lockbits 而不是 getpixel,但同样的问题仍然存在。

4

0 回答 0