C#中GetPixel在Valorant运行时无法获取屏幕像素准确颜色问题
问题
使用GetPixel获取屏幕像素颜色时,返回结果与真实值存在细微偏差。例如VS Studio背景色为(31, 31, 31),但获取到的RGB数值会在28-34之间浮动。该问题仅在运行Valorant时出现,即使Valorant处于最小化非全屏状态。
复现代码
using System; using System.Drawing; using System.Runtime.InteropServices; using System.Threading; // 补充缺失的命名空间,否则Thread无法使用 namespace nameherecool { sealed class Win32 { [DllImport("user32.dll")] static public extern bool SetProcessDPIAware(); [DllImport("user32.dll")] static extern IntPtr GetDC(IntPtr hwnd); [DllImport("user32.dll")] static extern Int32 ReleaseDC(IntPtr hwnd, IntPtr hdc); [DllImport("gdi32.dll")] static extern uint GetPixel(IntPtr hdc, int nXPos, int nYPos); static public System.Drawing.Color GetPixelColor(int x, int y) { IntPtr hdc = GetDC(IntPtr.Zero); uint pixel = GetPixel(hdc, x, y); ReleaseDC(IntPtr.Zero, hdc); Color color = Color.FromArgb((int)(pixel & 0x000000FF), (int)(pixel & 0x0000FF00) >> 8, (int)(pixel & 0x00FF0000) >> 16); return color; } } class Program { static void Main(string[] argfs) { Win32.SetProcessDPIAware(); // 补充调用DPI适配,避免高DPI下坐标偏差 while (true) { Console.SetCursorPosition(0, 0); Console.WriteLine(Win32.GetPixelColor(0, 0) + " "); Thread.Sleep(500); } } } }
分析与解决方案
核心原因
问题的根源是Valorant的反作弊系统Vanguard。Vanguard在后台运行时,会对系统级的图形API做钩子或拦截,目的是防止外挂通过读取屏幕像素获取游戏信息。哪怕游戏处于最小化状态,Vanguard的后台服务仍在运行,会干扰GDI(GetPixel属于GDI API)的像素读取流程,导致返回值出现波动。
可行的解决方法
改用更底层的屏幕捕获API
放弃GDI的GetPixel,使用Windows提供的Windows.Graphics.CaptureAPI(适用于Win10 1903及以上版本)或者DirectX的屏幕捕获方法。这些API绕开了GDI的拦截,受Vanguard影响更小。确保程序拥有足够权限
以管理员权限运行程序,部分情况下Vanguard对低权限进程的拦截会更严格,提升权限可能减少读取偏差。调整Vanguard的运行状态
若不需要玩Valorant,可以在任务管理器中结束Vanguard的后台服务(vgc.exe),但重启后服务会自动恢复。注意:禁用Vanguard会导致无法运行Valorant。
内容的提问来源于stack exchange,提问作者sandwichwasher
相关产品推荐
相关产品推荐

