Lehel Posted August 2, 2004 Posted August 2, 2004 I created a form with a Panel control that I can draw in. What I'm 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 isn't 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 i'm not even sure if this next part is correct: Color c = Color.FromArgb(nRGB); Is there a more simple way of doing what I'm trying to do circumventing the need for PInvoke? Also, I was looking at the actual platform sdk GetPixel function and I can't figure out how I'm 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 Quote
Administrators PlausiblyDamp Posted August 3, 2004 Administrators Posted August 3, 2004 try [DllImport("gdi32.dll")] static extern uint GetPixel(IntPtr hdc, int nXPos, int nYPos); Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
Lehel Posted August 3, 2004 Author Posted August 3, 2004 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 can't 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: Quote
Recommended Posts