color values different per monitor with getpixel/lockbits

170 views Asked by At

Im trying to make a method that returns the color value of a specific pixel. The problem i am having it that the same piece of code returns a different ARGB on different monitors.

Currently im using this method:

    public static Color GetColorFromPosition(string process, int x, int y)
    {
        //using bitmap object to do actions
        using (Bitmap Capture = CaptureApplication(Path.GetFileNameWithoutExtension(process)))
        {
            //copy specific part of screen and push it into bmp
            Color result = Capture.Clone(new Rectangle(x, y, 1, 1), Capture.PixelFormat).GetPixel(0,0);

            return result;
        }
    }

The captureapplication method looks like this (method takes a snapshot of an open application):

    public static Bitmap CaptureApplication(string procName)
    {
        Rectangle clientArea = GetProcessWindowRect(procName);

        Bitmap bp = new Bitmap(clientArea.Width, clientArea.Height, PixelFormat.Format32bppArgb);
        Graphics g = Graphics.FromImage(bp);
        g.CopyFromScreen(clientArea.Left, clientArea.Top, 0, 0, Screen.PrimaryScreen.Bounds.Size, CopyPixelOperation.SourceCopy);

        return bp;
    }

This captures an application window thats open and then gets the ARGB value of the given pixel using getpixel. So to give an example i want the color at x=100 and y=100. On computer one it would return [A=255, R=6, G=158, B=12] and on the other computer i get [A=255, R=6, G=157, B=13]. The results are slightly different.

My question is why does this happen and it there a solution? I've also tried using lockbits instead of getpixel but the same problem persists.

0

There are 0 answers