繁体   English   中英

从外部程序读取了错误的像素颜色,需要偏移吗?

[英]Wrong pixel colors are being read from external program, Offset needed?

我正在尝试从外部程序读取像素,然后获取其RGB颜色。 每当我用鼠标找到位置并提取像素颜色时,此方法就可以正常工作。 但是,当我尝试从控制台程序执行此操作时,RBG颜色会返回。

我相信可能会丢失偏移量,因此,每当我使用鼠标找到位置时,它就会使用屏幕像素,而当我使用下面的功能激活外部程序时,它将从该窗口句柄中获取像素位置。

也可能是因为它是一款游戏,而且绘制方式有所不同,有什么秘诀吗? 如果我尝试从记事本中获取像素颜色,则可以使用。

[DllImport("user32.dll")] static extern bool SetForegroundWindow(IntPtr hWnd);
public static void Activate(string processName = "CookieGame")
{
     var processes = Process.GetProcessesByName(processName);
     var process = processes.FirstOrDefault();
     if (process != null)
     {
       SetForegroundWindow(process.MainWindowHandle);
     }
}

我使用以下函数从某个位置提取像素颜色,这是在将程序设置为活动窗口后运行的(上面的函数):

public class MailReader
    {
[DllImport("user32.dll")] public static extern bool GetCursorPos(ref Point lpPoint);

[DllImport("gdi32.dll", CharSet = CharSet.Auto, SetLastError = true, ExactSpelling = true)] public static extern int BitBlt(IntPtr hDC, int x, int y, int nWidth, int nHeight, IntPtr hSrcDC, int xSrc, int ySrc, int dwRop);


        static Bitmap screenPixel = new Bitmap(1, 1, PixelFormat.Format32bppArgb);
        public static Color GetColorAt(Point location)
        {
            using (Graphics gdest = Graphics.FromImage(screenPixel))
            {
                using (Graphics gsrc = Graphics.FromHwnd(IntPtr.Zero)){
                    IntPtr hSrcDC = gsrc.GetHdc();
                    IntPtr hDC = gdest.GetHdc();
                    int retval = BitBlt(hDC, 0, 0, 1, 1, hSrcDC, location.X, location.Y, (int)CopyPixelOperation.SourceCopy);
                    gdest.ReleaseHdc();
                    gsrc.ReleaseHdc();
                }
            }

            return screenPixel.GetPixel(0, 0);
        }
    }

正在运行Conole程序,这将返回我告诉它的正确X和Y像素,但是颜色又变回来了:

namespace TestProgram.TestConsole
{
    class Program
    {
        static void Main(string[] args)
        {

            var model = new PixelInformation
            {
                X = 505,
                Y = 27,
                R = 117,
                G = 208,
                B = 50
            };
            var point = new Point();
            point.X = model.X;
            point.Y = model.Y;

            ActivateWindow.Activate("cookieGame");
            var location = PixelReader.GetColorAt(point);
            Console.WriteLine("Position X: " + point.X + " Y: " + point.Y);

            Console.WriteLine("R:" + location.R + " " + "G:" + location.G + " B:" + location.B);

            Console.ReadKey();
        }
    }
}

这些症状与使用100%以外的字体缩放比例的系统一致,并且该过程不支持DPI。 因此,该过程需要进行DPI虚拟化。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM