Problem with DllImport
-
Iam trying to get the pixel of a area in a screen of my program (for collison detection) the API call (GetPixel) seems to work..and it returns a int that i use with Color.FromArgb(int) (i think thats it) anyways when i try to convert the int i get back the color but...it doesnt convert correctly.....the A(alpha) of the color is always 0. Is there something i need to do before i try to convert the int ? Here is the code i use.
[DllImport("Gdi32.dll")] private static extern int GetPixel(IntPtr Handler,int x,int y); [DllImport("user32.dll")] private static extern IntPtr GetDesktopWindow(); // [DllImport("user32.dll")] private static extern IntPtr GetWindowDC(int Pointer); [DllImport("user32.dll")] private static extern IntPtr ReleaseDC(IntPtr hWnd,IntPtr hdc); //Use a method Called check pixel (overloads Int X, Int Y) int j = CheckPixel(100,100); int jm =Convert.ToInt32(j); Color c = Color.FromArgb(jm); MessageBox.Show(c.ToString()); //method CheckPixel private int CheckPixel(int x,int y) { IntPtr DC = GetWindowDC(GetDesktopWindow().ToInt32()); int jm = GetPixel(DC,x,y); // // ReleaseDC(GetDesktopWindow(),DC); // return jm; }
Thanks for your time. Jesse M The Code Project Is Your Friend... -
Iam trying to get the pixel of a area in a screen of my program (for collison detection) the API call (GetPixel) seems to work..and it returns a int that i use with Color.FromArgb(int) (i think thats it) anyways when i try to convert the int i get back the color but...it doesnt convert correctly.....the A(alpha) of the color is always 0. Is there something i need to do before i try to convert the int ? Here is the code i use.
[DllImport("Gdi32.dll")] private static extern int GetPixel(IntPtr Handler,int x,int y); [DllImport("user32.dll")] private static extern IntPtr GetDesktopWindow(); // [DllImport("user32.dll")] private static extern IntPtr GetWindowDC(int Pointer); [DllImport("user32.dll")] private static extern IntPtr ReleaseDC(IntPtr hWnd,IntPtr hdc); //Use a method Called check pixel (overloads Int X, Int Y) int j = CheckPixel(100,100); int jm =Convert.ToInt32(j); Color c = Color.FromArgb(jm); MessageBox.Show(c.ToString()); //method CheckPixel private int CheckPixel(int x,int y) { IntPtr DC = GetWindowDC(GetDesktopWindow().ToInt32()); int jm = GetPixel(DC,x,y); // // ReleaseDC(GetDesktopWindow(),DC); // return jm; }
Thanks for your time. Jesse M The Code Project Is Your Friend...Quote from MSDN: "GetPixel: The GetPixel function retrieves the red, green, blue (RGB) color value of the pixel at the specified coordinates. " This seems to imply that the Alpha value is undefined when returned from
GetPixel
. It seems as though the Alpha is always zero, but then the docs don't say that so you can't trust it. I haven't been able to locate how to get the alpha value though :( -Nathan --------------------------- Hmmm... what's a signature?