在C#(全屏游戏)中获取中间屏幕像素(颜色)?
我正在运行一个全屏游戏,我试图找出中间像素的颜色。但是,我使用的代码似乎只适用于窗口应用程序/游戏等。这是我的代码:在C#(全屏游戏)中获取中间屏幕像素(颜色)?,c#,colors,fullscreen,pixel,intptr,C#,Colors,Fullscreen,Pixel,Intptr,我正在运行一个全屏游戏,我试图找出中间像素的颜色。但是,我使用的代码似乎只适用于窗口应用程序/游戏等。这是我的代码: public static 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
public static 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;
}
我得到的中间屏幕像素如下:
int ScreenWidth = System.Windows.Forms.Screen.PrimaryScreen.Bounds.Width;
int ScreenHeight = System.Windows.Forms.Screen.PrimaryScreen.Bounds.Height;
那么我怎样才能使这段代码与全屏游戏兼容呢?
它给我一个ARGB值A=255,R=0,G=0,B=0
,即使我100%肯定中间屏幕像素是红色的。关于:
//using System.Windows.Forms;
public static Color GetPixelColor(int x, int y)
{
Bitmap snapshot = new Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height, PixelFormat.Format32bppArgb);
using(Graphics gph = Graphics.FromImage(snapshot))
{
gph.CopyFromScreen(Screen.PrimaryScreen.Bounds.X, Screen.PrimaryScreen.Bounds.Y, 0, 0, Screen.PrimaryScreen.Bounds.Size, CopyPixelOperation.SourceCopy);
}
return snapshot.GetPixel(x, y);
}
然后:
首先,你的RGB参数是按相反的顺序排列的(BGR)。请不要发布重复的问题:我很抱歉,但我非常渴望尽快解决这个问题。我将删除另一个。很抱歉回复太晚,但我这样做是为了一个游戏,这个方法被放入一个while循环中,每个循环之间有30毫秒的延迟,所以基本上每30毫秒我都会拍一张快照,这会使游戏滞后(以及我的整个电脑)。难道没有别的办法吗?
Color middleScreenPixelColor = GetPixelColor(Screen.PrimaryScreen.Bounds.Width/2, Screen.PrimaryScreen.Bounds.Height/2);