RGB from GetPixel, using user32.dll 
Author Message
 RGB from GetPixel, using user32.dll

How to get the RGB (hex) value from a long, returned by GetPixel?

using System.Runtime.InteropServices;
...

[DllImport("gdi32", CharSet=CharSet.Auto)]
static extern long GetPixel( long hDc, long x, long y );

[DllImport("user32.dll", CharSet=CharSet.Auto)]
static extern IntPtr GetDesktopWindow();

[DllImport("user32.dll", CharSet=CharSet.Auto)]
static extern long GetDC(IntPtr hwnd);

...

long hDc = GetDC( GetDeskTopHwnd() );
long pixCol = GetPixel( hDc, x, y );
...

//How to get the RGB color from pixCol?
//It must be something like ?!?
/*
long BVal = pixCol / 65536;
long GVal = (pixCol - (65536 * BVal)) / 256;
long RVal = pixCol - (65536 * BVal + 256 * GVal);
*/



Sat, 28 Feb 2004 17:23:27 GMT  
 RGB from GetPixel, using user32.dll

Quote:
>[DllImport("gdi32", CharSet=CharSet.Auto)]
>static extern long GetPixel( long hDc, long x, long y );

>[DllImport("user32.dll", CharSet=CharSet.Auto)]
>static extern IntPtr GetDesktopWindow();

>[DllImport("user32.dll", CharSet=CharSet.Auto)]
>static extern long GetDC(IntPtr hwnd);

All longs above should be ints.

Quote:
>//How to get the RGB color from pixCol?

Color c = ColorTranslator.FromWin32( pixCol );

Now check the R, G and B properties of c.

Mattias

====================================

http://www.msjogren.net/dotnet/

CodeHound - The Software Developer's Search Engine
http://www.codehound.com



Sat, 28 Feb 2004 19:51:01 GMT  
 RGB from GetPixel, using user32.dll
Thanks Mattias, but even afther these changes, it still not works :(
I always get 255,255,255 back on my Windows XP machine, even if I change
the pixel location...

Here is the code I use right now:

using System;
using System.Drawing;
using System.Runtime.InteropServices;

public class DesktopColor
{

 [DllImport("user32.dll", CharSet=CharSet.Auto)]
 static extern IntPtr GetDesktopWindow();

 [DllImport("user32.dll", CharSet=CharSet.Auto)]
 static extern int GetDC(IntPtr hwnd);

 [DllImport("user32.dll", CharSet=CharSet.Auto)]
 static extern void ReleaseDC( IntPtr hwnd, int hDc );

 [DllImport("gdi32", CharSet=CharSet.Auto)]
 static extern int GetPixel( int hDc, int x, int y );

 private string GetRGB(int Dec){
  Color c = ColorTranslator.FromWin32( Dec );
  return c.R.ToString() + "," + c.G.ToString() + "," + c.B.ToString();
 }

 public string Test() {
  IntPtr DesktopHwnd = GetDesktopWindow();
  int hDc = GetDC( DesktopHwnd );
  int pix = GetPixel( hDc, 100, 100 );
  ReleaseDC( DesktopHwnd , hDc);
  return GetRGB(pix);
 }

Quote:
}



Sat, 28 Feb 2004 21:08:55 GMT  
 RGB from GetPixel, using user32.dll

Quote:
> public string Test() {
>  IntPtr DesktopHwnd = GetDesktopWindow();
>  int hDc = GetDC( DesktopHwnd );
>  int pix = GetPixel( hDc, 100, 100 );
>  ReleaseDC( DesktopHwnd , hDc);
>  return GetRGB(pix);
> }
>}

Try skipping GetDesktopWindow, and instead do

  int hDc = GetDC( IntPtr.Zero );
  int pix = GetPixel( hDc, 100, 100 );
  ReleaseDC( IntPtr.Zero , hDc);
  return GetRGB(pix);

BTW, ReleaseDC should have int as the return type, not void.

Mattias

====================================



Sun, 29 Feb 2004 02:19:53 GMT  
 RGB from GetPixel, using user32.dll
Thanks! Your sollutions works.

With kind regards,

Marco Tenback


Quote:
> > public string Test() {
> >  IntPtr DesktopHwnd = GetDesktopWindow();
> >  int hDc = GetDC( DesktopHwnd );
> >  int pix = GetPixel( hDc, 100, 100 );
> >  ReleaseDC( DesktopHwnd , hDc);
> >  return GetRGB(pix);
> > }
> >}

> Try skipping GetDesktopWindow, and instead do

>   int hDc = GetDC( IntPtr.Zero );
>   int pix = GetPixel( hDc, 100, 100 );
>   ReleaseDC( IntPtr.Zero , hDc);
>   return GetRGB(pix);

> BTW, ReleaseDC should have int as the return type, not void.

> Mattias

> ====================================




Sun, 29 Feb 2004 17:32:08 GMT  
 
 [ 5 post ] 

 Relevant Pages 

1. user32.dll, user32.lib, and wacky problem

2. user32.dll, user32.lib, and wacky problem

3. Why the RGB value I get from Getpixel() is different from the value from the bit

4. loading symbols on xp (user32 and nvoglnt.dll)

5. Access violation in user32.dll in debug mode

6. User32.dll error

7. LNK2001 Error and Platform Specific user32.dll

8. ATL COM object hangs on user32.dll MessageBoxA() call

9. IsHungAppWindow function in User32.dll ?

10. How Can we Read The Screen Contents Without Using GetPixel

11. ATL COM object hangs on user32.dll MessageBoxA() call

12. User32.dll error

 

 
Powered by phpBB® Forum Software