#Can't get pixel RGB value..always resolves to white

1 messages · Page 1 of 1 (latest)

fresh talon
past tangle
#

It's not quite clear what the heck is going on in that code. You iterate the array to sample different pixels on the screen(with some magic numbers) and save an int(?) As an element of rgbs.
Then you iterate it again, while using the index both to set differentelements in a separate array as well as to sample different color channels.
Nothing in that makes any sense to me. At least without knowing the context.

fresh talon
#
using System;
using System.Runtime.InteropServices;
using System.Runtime.CompilerServices;

public static class PInvoke
{
    [DllImport("user32", ExactSpelling = true)]
    public static extern IntPtr GetDesktopWindow();

    [DllImport("user32", ExactSpelling = true)]
    public static extern IntPtr GetDC(IntPtr hWnd);

    [DllImport("gdi32", ExactSpelling = true)]
    public static extern uint GetPixel(IntPtr hdc, int x, int y);

    [MethodImpl(MethodImplOptions.AggressiveInlining)]
    public static byte GetRValue(uint rgb) => (byte)((ushort)rgb & 0xFF);

    [MethodImpl(MethodImplOptions.AggressiveInlining)]
    public static byte GetGValue(uint rgb) => (byte)((ushort)(rgb >> 8) & 0xFF);

    [MethodImpl(MethodImplOptions.AggressiveInlining)]
    public static byte GetBValue(uint rgb) => (byte)((ushort)(rgb >> 16) & 0xFF);
}
#

here's PInvoke.. this is code zombie gave to me

#

I'll comment it (the first one)

past tangle
#

Nothing in that code answers any of my questions.

fresh talon
#

I'm starting to realize how confusing these names are

#

I'm going to rewrite it

#

rewrote it

#

yeah even in build it says pixel at 500 x 500 is always white

#

or at least it says so

#

unfortunately I have no idea what these parameters want me to put in

past tangle
#
private Color32 SampleScreenColor(int x, int y)
    {
        uint encodedColor = PInvoke.GetPixel(PInvoke.GetDesktopWindow(), x, y);

        int  r = PInvoke.GetRValue(encodedColor);
        int g = PInvoke.GetGValue(encodedColor);
        int b = PInvoke.GetBValue(encodedColor);
        Color32 color = new Color32(r,g, b);
        return color
    }
#

Here's how you get a color of 1 pixel if I get the api right.

fresh talon
fresh talon
past tangle
#

I typed it on my phone. I don't have access to ide ATM.

#

You should be able to solve small issues like that with an ide.

fresh talon
#

Does it need to be Color32?

#

I don't see a method implemented for getting the alpha in PInvoke

past tangle
#

You can't get the alpha. Screen pixels don't have alpha

#

Just set it to 1

fresh talon
#

Ah, ok

past tangle
#

And you can cast color 32 to regular color.

#

You have to use 32 there , because the values are ranging from 0 to 255

#

As opposed to 0 to 1 of regular color

fresh talon
#
    private Color32 SampleScreenColor(int x, int y)
    {
        uint encodedColor = PInvoke.GetPixel(PInvoke.GetDesktopWindow(), x, y);

        byte r = PInvoke.GetRValue(encodedColor);
        byte g = PInvoke.GetGValue(encodedColor);
        byte b = PInvoke.GetBValue(encodedColor);
        Color32 color = new Color32(r, g, b, 1);
        return color;
    }

alright, ended up with this

#

Color32 requires byte so I switched int to byte

#

(also the PInvoke methods return a byte anyway)

past tangle
#

Yeah, that's fine.

fresh talon
#

something I noticed

#

It doesn't actually return pure white, it returns off-white/gray

#

I'm calling SampleScreenColor with random coordinates everytime I press the button so I shouldn't be getting the exact same color every time

#

still no errors or warnings

#

yep, both methods return exactly the same color

#

I'm kind of lost.. I have no idea why its not working

#

I know GetPixel works on my computer because I had a service before that used it that was able to get my screen pixels before

#

If it's unable to get the pixels it should be black by default no?

loud bloom
#

GetPixel takes a device context, you're giving a window handle. So that's one thing

past tangle
#

Yeah, I'm pretty sure the issue is with the GetPixel method.

fresh talon
#

ok now I just need to know what to put in

#

h? device context

loud bloom
#

get dc from hWND

fresh talon
#

wrong ss

fresh talon
loud bloom
#

window handle

#

var hDC = PInvoke.GetDC(PInvoke.GetDesktopWindow());