#Can't get pixel RGB value..always resolves to white
1 messages · Page 1 of 1 (latest)
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.
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)
Nothing in that code answers any of my questions.
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
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.
I found https://www.pinvoke.net/default.aspx/gdi32/GetPixel however none of the variable names are elaborated on
Color32 requires alpha channel
Then add it
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.
Does it need to be Color32?
I don't see a method implemented for getting the alpha in PInvoke
Ah, ok
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
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)
Yeah, that's fine.
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?
GetPixel takes a device context, you're giving a window handle. So that's one thing
Yeah, I'm pretty sure the issue is with the GetPixel method.
ah so thats what DC means
ok now I just need to know what to put in
h? device context
get dc from hWND
wrong ss
what is hWND?