using GetPixel

Lehel

New member
Joined
Aug 2, 2004
Messages
2
I created a form with a Panel control that I can draw in. What Im trying to do is scan different pixels within the panel to see what RGB value they have.

I did something like this:

[DllImport("gdi32")]
[return : MarshalAs(UnmanagedaType.U4)] //i guess this part isnt necessary
public static extern int GetPixel (IntPtr hdc, int x, int y);

...

Graphics g = Graphics.FromHwnd(mypanel.Handle);

int nRGB = GetPixel(g.GetHdc(), 10, 10); //nRGB returns -1?!

//now im not even sure if this next part is correct:
Color c = Color.FromArgb(nRGB);


Is there a more simple way of doing what Im trying to do circumventing the need for PInvoke? Also, I was looking at the actual platform sdk GetPixel function and I cant figure out how Im using it incorrectly. The documentation talks about making sure that the x,y of the pixel is within the clipping region. I set a breakpoint and looked at g.VisibleClipBounds and it showed the dimensions of the panel so that seemed okay. I even checked the GetDeviceCaps which returned the RC_BITBLT bit on so everything should be fine and good in the neighborhood...but I still get that blased -1 value. What could be my trouble?

thanks.
Lehel
 
figured out my problem

thanks for the help, but the problem was how i was using the api call. GetPixel only works with bitmaps. I cant use it on any device context for a window to get a pixel color. If I want to do this, the circumvention is do the screen capture trick using calls such as CreateCompatibleDC, CreateCompatibleBitmap, SelectObject and BitBlt to basically BitBlt window contents into a bitmap with a compatible DC. then use the GetPixel on the compatible DC and there we go. :rolleyes:
 
Back
Top