|
-
Aug 25th, 2006, 09:33 AM
#1
Thread Starter
Junior Member
[RESOLVED] help: find a pixel on screen by its color
ok imagine that my entire screen was blue and one pixel was red. how can i find this pixel?
is there a way to scan through every pixel then when the pixel u want is found , store its location in a variable?
thanks
-
Aug 25th, 2006, 09:54 AM
#2
Re: help: find a pixel on screen by its color
Take a look at http://www.allapi.net/apilist/apilist.php for GetPixel/GetWindowDC/GetDesktopWindow/GetDIBits/BitBlt APIs.
Easiest (but very slow) way is using the GetPixel API.
Something like this:
VB Code:
lColor = GetPixel(GetWindowDC(GetDesktopWindow()), x, y )
Or, you can do it by modifying the code from GetDIBits's example. It will be a LOT faster.
-
Aug 25th, 2006, 10:32 AM
#3
Thread Starter
Junior Member
Re: help: find a pixel on screen by its color
this works for entire screen and not just restricted to the dymentions of program form?
-
Aug 25th, 2006, 10:42 AM
#4
Re: help: find a pixel on screen by its color
Both codes should work for entire screen.
Do you want it for a particular form only ?
-
Aug 25th, 2006, 01:26 PM
#5
Thread Starter
Junior Member
Re: help: find a pixel on screen by its color
nope 
thanks for help and links. ill check em' out.
-
Aug 26th, 2006, 02:41 AM
#6
Thread Starter
Junior Member
Re: help: find a pixel on screen by its color
when i run the below code. the returned color is 16382453
however i dont know what that means
i tried spliting it up into RGB but that didtn work cas it went over 255
163 824 53
163 82 453
16 382 453
so what does 16382453 mean?
thx
VB Code:
Private Declare Function GetDesktopWindow Lib "user32" () As Long
Private Declare Function GetPixel Lib "gdi32" (ByVal hdc As Long, ByVal x As Long, ByVal y As Long) As Long
Private Declare Function GetWindowDC Lib "user32" (ByVal hwnd As Long) As Long
Private Sub Form_Load()
Dim dhwnd As Long
Dim dhdc As Long
Dim color As Long
Dim x As Long
Dim y As Long
dhwnd = GetDesktopWindow
dhdc = GetWindowDC(dhwnd)
color = GetPixel(dhdc, 0, 0)
Debug.Print color
End Sub
Last edited by Cipherman; Aug 26th, 2006 at 03:29 AM.
-
Aug 26th, 2006, 03:47 AM
#7
Re: help: find a pixel on screen by its color
 Originally Posted by Cipherman
ive often seen the hDC thing in api calls but not entirely sure what it means.
in this example
lColor = GetPixel(GetWindowDC(GetDesktopWindow()), x, y )
does it mean it will only search pixels on the desktop
-
and in this example
ColorMe = GetPixel(hDC, X, Y)
does it mean it will search pixels on the current screen?
or do i have to change the hDC to something else to do that?
 Originally Posted by MSDN
hDc: This property is a Windows operating environment device context handle. The Windows operating environment manages the system display by assigning a device context for the Printer object and for each form and PictureBox control in your application. You can use the hDC property to refer to the handle for an object's device context. This provides a value to pass to Windows API calls.
You can think it as a drawing board. If your window/control/printer has an hDc, whatever you 'draw' on its hDc, it appears on the screen/printed page.
Note: Some controls like Label or Image controls don't have any DC. They are painted direcly on the form. They are called lightweight controls.
Again, some controls, like the Frame control, don't expose the hDC property in VB IDE. You'll not see it in the intellisence list or in the object browser. But you can access it using the GetDC API.
GetPixel gets the specified pixel of a given hDc (in other words, a particular pixel of a window)
For your own form/Picturebox etc, you can get the hDc from the .hDc property.
For getting the hDc of an external window we have 2 APIs:
 Originally Posted by MSDN
GetDC: The GetDC function retrieves a handle to a display device context (DC) for the client area of a specified window or for the entire screen. You can use the returned handle in subsequent GDI functions to draw in the DC.
And,
 Originally Posted by MSDN
GetWindowDC: The GetWindowDC function retrieves the device context (DC) for the entire window, including title bar, menus, and scroll bars. A window device context permits painting anywhere in a window, because the origin of the device context is the upper-left corner of the window instead of the client area.
You can find their full documentation in your MSDN CD or here
The desktop itself is a window. It has valid hWnd (Window Handel) and valid DC.
So, in order to get a particular pixel from it's DC, we need to first get its hWnd by using the GetDesktopWindow API.
Then we need to get its dc using the GetWindowDC API.
Finally, we need to get the pixel's color value using the GetPixel API.
If we break the code,
lColor = GetPixel(GetWindowDC(GetDesktopWindow()), x, y )
it will be like,
VB Code:
Dim lDesktopHwnd As Long
Dim lDesktopDc As Long
Dim lColor As Long
lDesktopHwnd = GetDesktopWindow() 'get hWnd of the desktop
lDesktopDc = GetWindowDC(lDesktopHwnd) 'get the hDc of the desktop
lColor = GetPixel(lDesktopDc, x, y) 'get the color of specified pixel
Hope it helps. 
Edit: Hey ! You changed the complete post.
Last edited by iPrank; Aug 26th, 2006 at 03:52 AM.
-
Aug 26th, 2006, 04:08 AM
#8
Re: help: find a pixel on screen by its color
 Originally Posted by Cipherman
when i run the below code. the returned color is 16382453
however i dont know what that means
i tried spliting it up into RGB but that didtn work cas it went over 255
163 824 53
163 82 453
16 382 453
so what does 16382453 mean?
thx
colors in Windows (or VB) is represented by Long integers (32bit).
The value 16382453 (or F9F9F5 in Hex) is the value of this color.
After the debug.print line add,
Me.BackColor = color
it will change the form's background color to the color of the specified pixel.
For VB's long to RGB conversion, you'll need to extract each color from the long value.
There are many example on the web: http://www.google.com/search?client=...utf-8&oe=utf-8
Last edited by iPrank; Aug 26th, 2006 at 04:14 AM.
-
Aug 26th, 2006, 05:03 AM
#9
Thread Starter
Junior Member
Re: help: find a pixel on screen by its color
many thanks to both of you. im glad someone got to my first question before i edited it
-
Aug 26th, 2006, 06:04 AM
#10
Re: help: find a pixel on screen by its color
 Originally Posted by Cipherman
many thanks to both of you. im glad someone got to my first question before i edited it

Both of me are glad to help.
-
Aug 26th, 2006, 06:10 AM
#11
Thread Starter
Junior Member
Re: help: find a pixel on screen by its color
>.<
i got some code which worked for me
VB Code:
Public Sub LongToRGB(ByVal lngColor As Long, intRed As Integer, intGreen As Integer, intBlue As Integer)
intRed = lngColor Mod &H100
lngColor = lngColor \ &H100
intGreen = lngColor Mod &H100
lngColor = lngColor \ &H100
intBlue = lngColor Mod &H100
End Sub
many thank iPrank
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|