简体   繁体   English

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

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

I'm trying to read a pixel from an external program and then get its RGB colors. 我正在尝试从外部程序读取像素,然后获取其RGB颜色。 This works flawlessly whenever I find the location with the mouse and extract the pixel colors. 每当我用鼠标找到位置并提取像素颜色时,此方法就可以正常工作。 However when I try to do it from a console program, the RBG colors comes back.. differently than what I expected. 但是,当我尝试从控制台程序执行此操作时,RBG颜色会返回。

I believe it could be an offset missing, so whenever I find the location using my mouse it's using my screens pixel, and whenever I activate the external program using the function below it will take the pixel locations from that window handle. 我相信可能会丢失偏移量,因此,每当我使用鼠标找到位置时,它就会使用屏幕像素,而当我使用下面的功能激活外部程序时,它将从该窗口句柄中获取像素位置。

It could also be something about it being a game, and it's getting drawn differently, any tips? 也可能是因为它是一款游戏,而且绘制方式有所不同,有什么秘诀吗? If I try to get the pixel colors from notepad it works. 如果我尝试从记事本中获取像素颜色,则可以使用。

[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);
     }
}

I use the following function for extracting pixel colors from a location, this is run after I've set the program as active window (function above): 我使用以下函数从某个位置提取像素颜色,这是在将程序设置为活动窗口后运行的(上面的函数):

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 program being run, this returns the correct X and Y pixels that I told it to, but the colors come back off: 正在运行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();
        }
    }
}

These symptoms are consistent with your system using font scaling other than 100% and the process not being DPI aware. 这些症状与使用100%以外的字体缩放比例的系统一致,并且该过程不支持DPI。 Thus the process is subject to DPI virtualization. 因此,该过程需要进行DPI虚拟化。

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

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