It really doesn't matter, either the whole screen or from the window. The window will take up the whole screen, and that would probably be the simpler route to go. Or anyways I hope it is.
If anyone could please submit sample code on how to get the color of a pixel from lets say location 450, 325 on the screen I would really appreciate it. I know there is some information there, but I don't have a clue where to begin.
Sorry if I am asking for too much. I am really interested in this.
Dim Red As Long = Colour And 255) Dim Green As Long = Int((Colour/ 256) And 255) Dim Blue As Long = Int((Colour/65536) And 255)
Colour = Red Or (Green * 256) Or (Blue * 65536)
VB does many things: 1. Will automatically attempt to assemble the entire thing in 32-bit code; therefore, using Long won't hurt anything, and make transformations quicker. 2. Will "see" that you are dividing by a base 2 value and transform that into Shifts in assembly, so even though dividing is expensive, those commands are not. 3. "Int" function is a caster in VB in such a way that it does not place the divided function into the FPU unit, saving precious cycles. 4. "And" of 255 will cast it as a byte. 5. RGB is in reverse order, I'm pretty sure (at least VB5 and 6 were).
So, in short, those functions are as if you did this in C++:
Int Red = Colour && 255 Int Green = (Colour >> 8) && 255 Int Blue = (Colour >> 16) && 255
Colour = Red || (Green << 8) || (Blue << 16)
But VB goes further by eliminating the &&/And functions by making this in Assembly using optimizations:
Dim pixel as UInt = GetPixel(GetDC( ' pass either a handle here, or IntPtr.Empty for the desktop DC
), 13, 397)
Now, to turn this into RGB is another matter. In hex, the number is 0x00RRGGBB, that is, red, green, blue. Actually, it's probably 0x00BBGGRR. In any case, you need to do this:
dim blue as unsigned char = pixel AND 0xFF
dim green as unsigned char = (pixel AND 0xFF00) >> 2 ' I don't know how to do shift in VB
dim red as unsigned char = (pixel AND 0xFF0000) >> 4 ' I don't know how to do shift in VB
That will give you RGB values. Bear in mind, none of this is tested, but that's how the value coming back from GetPixel works, and how it gets turned into RGB values.
IntPtr.Zero is the same as NULL. Passing NULL to GetDC gets the desktop DC, I thought.
Yes, what you've been advised sounds good. I didn't realise that you had a specific window you wanted to get the value from. If you did this, then the pixel location would be 0,0 on the top left of the client window, not the whole screen.
Sorry I didn't reply sooner, I'm in Australia, and I was sleeping :-)
Private Declare Function GetDC Lib "user32" ( _
ByVal hwnd As Integer) As Integer
Private Declare Function GetPixel Lib "gdi32" ( _
ByVal hDC As Integer, _
ByVal x As Integer, _
ByVal y As Integer) As Integer
Private Declare Function ReleaseDC Lib "user32" ( _
ByVal hwnd As Integer, _
ByVal hdc As Integer) As Integer
Dim value As IntPtr
value = IntPtr.Zero
I dont' know what to do with this tho. I looked up IntPtr.Zero in help and it shows usage as:
Dim value As IntPtr
value = IntPtr.Zero
More help on this would be very appreciated. I really want to be able to get the color of a pixel on the screen in the vb.net language.
Where did you find this code What is it supposed to do Cut and paste of code you don't understand is always a bad idea.
hdc would be a device context, the thing that Windows must generate in order to draw something. GetPixel is returning the color of a pixel in a DC, build from a window, which would be a single number which, when converted to hex, is an RGB triple.
Can someone please explain to me how to use this code so that I can grab the pixel color from a location on the screen
Well, forget the session stuff, and instead just pass IntPtr.Zero ( from memory, it's NULL that you want ) to GetDC to get the DC of the screen. Then the GetPixel/GetDC/ReleaseDC code should be all that you need.
I know all of that. I use it to do screen wide color sampling for a neat little utility of mine that people love. It samples at a 100 ms rate and somehow noone seems to be strain from that.
Yah... I was referring to the API.....
Oh... and if you play your cards right... I'll send you the utility.
GetPixel is supported directly by the Bitmap class. However, this person is trying to use an API call of the same name, which means that pinvoke is involved, and it's a different API altogether.
As an aside, GetPixel is really slow, if you need to get a lot of pixels, you're better off writing a C# dll, given that VB.NET does not support unsafe blocks.
sometimes I don't take mywork seriously but when a friend saw this the other day he went crazy over it.......
And... wait until you see the Knowledge Navigator... it's really grown up however...
I'm getting into heavy stuff right now... I've got to figure out selection and cut and pastes and Url insertion inside the webbrowser control. Yuck!!!!!!
Let me know if you want the sources for what I'm about to send you.
The truth is, in this day and age, slow is a relative term. Get/SetPixel is still too slow for image processing, but there are applications where you wouldn't notice anymore.
I have been given the following advice. Can someone show me how this is done in code
-Use EnumWindows or FindWindow to get the handle of the target window -Pass the handle from EnumWindow or FindWindow to GetDC to get the hdc of that window -Pass the hdc from GetDC to GetPixel to get the pixel color
Getpixel API
Omego2K
"On a side-note though, I've tried using the SetPixel/GetPixel routines in VB2005, and they don't seem to work...oh well."
I use GetPixel all the time in Vb2005 and they work just fine.... however... I don't remember the code looking anything like cgraus is showing....
Paul Minnaar
It really doesn't matter, either the whole screen or from the window. The window will take up the whole screen, and that would probably be the simpler route to go. Or anyways I hope it is.
If anyone could please submit sample code on how to get the color of a pixel from lets say location 450, 325 on the screen I would really appreciate it. I know there is some information there, but I don't have a clue where to begin.
Sorry if I am asking for too much. I am really interested in this.
Pi Memorizer
Dim Red As Long = Colour And 255)
Dim Green As Long = Int((Colour/ 256) And 255)
Dim Blue As Long = Int((Colour/65536) And 255)
Colour = Red Or (Green * 256) Or (Blue * 65536)
VB does many things:
1. Will automatically attempt to assemble the entire thing in 32-bit code; therefore, using Long won't hurt anything, and make transformations quicker.
2. Will "see" that you are dividing by a base 2 value and transform that into Shifts in assembly, so even though dividing is expensive, those commands are not.
3. "Int" function is a caster in VB in such a way that it does not place the divided function into the FPU unit, saving precious cycles.
4. "And" of 255 will cast it as a byte.
5. RGB is in reverse order, I'm pretty sure (at least VB5 and 6 were).
So, in short, those functions are as if you did this in C++:
Int Red = Colour && 255
Int Green = (Colour >> 8) && 255
Int Blue = (Colour >> 16) && 255
Colour = Red || (Green << 8) || (Blue << 16)
But VB goes further by eliminating the &&/And functions by making this in Assembly using optimizations:
Mov EAX, Colour
XOr EBX, EBX
Mov BL, AL
Mov Red, EBX
Mov CL, 16d
Mov BL, AH
Mov Green, EBX
ShR EAX, CL
Mov BL, AL
Mov Blue, EBX
If no optimizing is involved, placing a few AND operations won't kill your program (espcially with now-a-days 3Ghz Dual processors and such).
On a side-note though, I've tried using the SetPixel/GetPixel routines in VB2005, and they don't seem to work...oh well.
-Rob
20+ Languages known and I'm "just a kid" still.
Feadan
The Knowledge Navigator Could you fill me in
Thanks in advance!
Elvis Silva
Dim pixel as UInt = GetPixel(GetDC( ' pass either a handle here, or IntPtr.Empty for the desktop DC
), 13, 397)
Now, to turn this into RGB is another matter. In hex, the number is 0x00RRGGBB, that is, red, green, blue. Actually, it's probably 0x00BBGGRR. In any case, you need to do this:
dim blue as unsigned char = pixel AND 0xFF
dim green as unsigned char = (pixel AND 0xFF00) >> 2 ' I don't know how to do shift in VB
dim red as unsigned char = (pixel AND 0xFF0000) >> 4 ' I don't know how to do shift in VB
That will give you RGB values. Bear in mind, none of this is tested, but that's how the value coming back from GetPixel works, and how it gets turned into RGB values.
Paradigm
IntPtr.Zero is the same as NULL. Passing NULL to GetDC gets the desktop DC, I thought.
Yes, what you've been advised sounds good. I didn't realise that you had a specific window you wanted to get the value from. If you did this, then the pixel location would be 0,0 on the top left of the client window, not the whole screen.
Sorry I didn't reply sooner, I'm in Australia, and I was sleeping :-)
Tryin2Bgood
// On a side-note though, I've tried using the SetPixel/GetPixel routines in VB2005, and they don't seem to work...oh well.
How don't they work
kgreer
Something like this
Private Declare Function GetDC Lib "user32" ( _ ByVal hwnd As Integer) As Integer Private Declare Function GetPixel Lib "gdi32" ( _ ByVal hDC As Integer, _ ByVal x As Integer, _ ByVal y As Integer) As Integer Private Declare Function ReleaseDC Lib "user32" ( _ ByVal hwnd As Integer, _ ByVal hdc As Integer) As Integer Dim value As IntPtrvalue = IntPtr.Zero
I dont' know what to do with this tho. I looked up IntPtr.Zero in help and it shows usage as:
Dim value As IntPtr
value = IntPtr.Zero
More help on this would be very appreciated. I really want to be able to get the color of a pixel on the screen in the vb.net language.
Thanks so much in advance!
Hugo Vale
Where did you find this code What is it supposed to do Cut and paste of code you don't understand is always a bad idea.
hdc would be a device context, the thing that Windows must generate in order to draw something. GetPixel is returning the color of a pixel in a DC, build from a window, which would be a single number which, when converted to hex, is an RGB triple.
Can someone please explain to me how to use this code so that I can grab the pixel color from a location on the screen
Well, forget the session stuff, and instead just pass IntPtr.Zero ( from memory, it's NULL that you want ) to GetDC to get the DC of the screen. Then the GetPixel/GetDC/ReleaseDC code should be all that you need.
Sean Bowen
christian,
I know all of that. I use it to do screen wide color sampling for a neat little utility of mine that people love. It samples at a 100 ms rate and somehow noone seems to be strain from that.
Yah... I was referring to the API.....
Oh... and if you play your cards right... I'll send you the utility.
Renee
Wamiq
GetPixel is supported directly by the Bitmap class. However, this person is trying to use an API call of the same name, which means that pinvoke is involved, and it's a different API altogether.
As an aside, GetPixel is really slow, if you need to get a lot of pixels, you're better off writing a C# dll, given that VB.NET does not support unsafe blocks.
jtwo
you are so cute ..... coming up......
sometimes I don't take mywork seriously but when a friend saw this the other day he went crazy over it.......
And... wait until you see the Knowledge Navigator... it's really grown up however...
I'm getting into heavy stuff right now... I've got to figure out selection and cut and pastes and Url insertion inside the webbrowser control. Yuck!!!!!!
Let me know if you want the sources for what I'm about to send you.
Renee
fc_curling
I don't see where I actually posted any code
The truth is, in this day and age, slow is a relative term. Get/SetPixel is still too slow for image processing, but there are applications where you wouldn't notice anymore.
Which card do I need to play :P
eric788
I have been given the following advice. Can someone show me how this is done in code
-Use EnumWindows or FindWindow to get the handle of the target window
-Pass the handle from EnumWindow or FindWindow to GetDC to get the hdc of that window
-Pass the hdc from GetDC to GetPixel to get the pixel color