[英]Get middle screen pixel (color) in C# (full screen game)?
我正在运行一个全屏游戏,并且试图找出中间像素的颜色。 但是,我使用的代码似乎仅适用于带窗口的应用程序/游戏/等。 这是我的代码:
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;
那么如何使此代码与全屏游戏兼容? 即使我100%肯定中间屏幕像素是红色A = 255, R = 0, G = 0, B = 0
它也为我提供了ARGB值: A = 255, R = 0, G = 0, B = 0
。
关于什么:
//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);
}
然后:
Color middleScreenPixelColor = GetPixelColor(Screen.PrimaryScreen.Bounds.Width/2, Screen.PrimaryScreen.Bounds.Height/2);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.