[I have also posted this in the C# forum as it applies to both languages - apologies if you've already seen this post!]

Hi,

I have a piece of C# code I found on the web to get a color at the pixel under the mouse pointer on the screen. It uses GDI32.dll and returns an integer which holds a windows COLORREF. For example if the pointer is over a pure white pixel the integer returned is 16777215 (0xFFFFFF). It works fine in C# but when I try to run the same code (after converting it to VB.Net of course) I always get the value -1. Why is this? Surely an integer in C# is the same as an integer in VB.Net - they both derive from the same class in the CLR don't they?

Anyway, the C# code to get the color is as follows:
<code>// The following goes in the declarations section...
[DllImport("gdi32.dll")]
public static extern int GetPixel(IntPtr hdc, int x, int y);

// ...then later I do this to get the colour under the current pixel.
int cr = GetPixel(hdcScreen, pt.X, pt.Y);</code>
The same code in VB.Net looks like this:
<code> 'Declare call the GDI32.DLL GetPixel function. Should return a valid integer.
<DllImport("gdi32.dll")> _
Public Shared Function GetPixel(ByRef hdc As IntPtr, ByVal x As Integer, ByVal y As Integer) As Integer

...etc...

'Get the colour at the current pixel.
Dim cr As Integer = GetPixel(hdcScreen, pt.X, pt.Y)

End Function</code>

Can anybody tell me why the VB code returns -1 and the C# code returns the correct value of 16777215? Do I need to do something else in my VB code like a cast or something?

Pretty confused here so any help would be appreciated!

TIA...

Mike.