簡體   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