Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

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

Posted

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:

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...