I'm trying to read the color information from a given pixel anywhere on the screen. I've tried it with GetPixel(h, x, y), but the result is always white. Is there another way, or how can I make it work (or already a topic about it that I missed?). I'm using Visual Studio 2008 bèta 2.
Retrieving pixel information
Collapse
X
-
Tags: None
-
I've tried this, but the when I try to run the program, I recieve the warning: "NullReferenceE xeption was unhandled."Yarr Of DoomComment
-
Now I've also tried 'Public bmp as new bitmap', but that only results in a compile error with the description: "Overload resolution failed because no accesible 'New' accepts this number of arguments."
I really need this function, so please somebody reply...
Yarr Of DoomComment
-
This reply is only posted to give this topic a little bit attention and not letting it go to page 2. I do this becuase I really need help with this and I can't continue working on my program without knowing how to get color information from a certain pixel on the screen...Comment
-
Hi,
Check this :
[code=vb]
Private Declare Function CreateDCA Lib "gdi32" (ByVal lpDriverName As String, lpDeviceName As Any, lpOutput As Any, lpInitData As Any) As Long
Private Declare Function DeleteDC Lib "gdi32" (ByVal hdc As Long) As Long
Private Declare Function GetPixel Lib "gdi32" (ByVal hdc As Long, ByVal x As Long, ByVal y As Long) As Long
Public Function MyPxInfo(ByVal x As Integer, ByVal y As Integer) As Long
DeskTopDC = CreateDCA("DISP LAY", ByVal 0&, ByVal 0&, ByVal 0&)
MyPxInfo = GetPixel(DeskTo pDC, x, y)
DeskTopDC = DeleteDC(DeskTo pDC)
End Function
[/code]
Call the function :
RGB = MyPxInfo(100,20 0)
Regards
VeenaComment
-
And yet again; Thanks Veena.
However, I don't really know how to convert the resulting values to seperate R,G and B values, does somebody know how to do this?
Yarr Of DoomComment
-
Make a new module and put this in it:
[code=vb]
Const C256 As Long = 256
Const C256Power2 As Long = (256 ^ 2)
'Return variables for after converting colour number to separate RGB values:
Global ReturnR As Long
Global ReturnG As Long
Global ReturnB As Long
Public Sub ColourToRGB(Col ourNumberToConv ert As Long)
'WHEN THIS FUNCTION HAS FINISHED, THESE VARIABLES WILL
'GIVE YOU THE R, G AND B VALUES OF THE ORIGINAL COLOUR NUMBER:
'ReturnR - 0 -> 255 = Red value
'ReturnG - 0 -> 255 = Green value
'ReturnB - 0 -> 255 = Blue value
Dim NumberLeftSoFar As Long
NumberLeftSoFar = ColourNumberToC onvert
ReturnB = Fix(NumberLeftS oFar / C256 / C256)
NumberLeftSoFar = NumberLeftSoFar - (ReturnB * C256Power2)
ReturnG = Fix(NumberLeftS oFar / C256)
ReturnR = NumberLeftSoFar - (ReturnG * C256)
End Sub
[/code]
Now you can run ColourToRGB(), then look at the 3 global variables ReturnR, ReturnG and ReturnB for the red, green and blue values, respectively.
ColourNumberToC onvert is the 'colour number' (I don't know what it's called - the long number given back by various things, such as BackColor, and the VB function RGB()).
Hope it's what you wanted! =D
(It has been optimized quite a bit for speed, by the way)
EDIT: That's for VB6! It should work for any programming language with very little modifications though!Comment
-
I'm sorry Robbie, but your code doesn't seem to help: the returned r value is -1, the returned g value is o, and b depends on the color.
Maybe I can write my own piece of code if someone could explain me the logic behind the returned long-value of Veena's code.
Yarr Of DoomComment
-
Originally posted by yarrofdoomI'm sorry Robbie, but your code doesn't seem to help: the returned r value is -1, the returned g value is o, and b depends on the color.
Maybe I can write my own piece of code if someone could explain me the logic behind the returned long-value of Veena's code.
Yarr Of Doom
Veena's MyPxInfo() is basically a partially pre-made version of GetPixel().
It is exactly the same as GetPixel(), except that with GetPixel, you give it the hDC of the object which you want to get the colour from, such as a PictureBox, and in Veena's code, that is automatically set to "DISPLAY", which is the entire screen.
So the values it gives back are exactly the same types. They are long values which are made like this:
Get the red, blue and green values of the pixel and convert each of them to binary.
Then put each of these 3 strings of 8 "0s or 1s" into this order:
BlueGreenRed
So you end up with a string of 24 0s and 1s, i.e. 24-bit binary. The right-most 8 bits are the red value from 0 to 255, the middle 8 bits are green and the left-most are the blue value.
This is even how it works when your display 'color depth' is set to 32-bit, which mine is. (i.e. It still works for me)
Here is a screenshot of a program I made, illustrating this:
I don't understand why it wouldn't work, other than GetPixel() not returning 24-bit values when not run on VB6...Comment
-
I've found why Robbie's code wouldn't work. When working with the second Visual Studio 2008 bèta, the GetPixel() and MyPxInfo() return a 64bit-value. Maybe it's a bug, or else it's a new way to display colors?Comment
-
Hi,
Just Check this out :
First Convert Long To Hex and extract RGB:
[code=vb]
Dim THex
Dim NewHex
Dim R, G, B
THex = Hex(LongColor)
Dim i As Byte
On Error Resume Next
THex = Right((THex), 6)
NewHex =THex
For i = 1 To (6 - Len(THex))
NewHex = NexHex & "0"
Next
R = CByte("&H" & Right$(NewHex, 2))
G = CByte("&H" & Mid$(NewHex, 3, 2))
B = CByte("&H" & Left$(NewHex, 2))
[/code]
Just Change "LongColor" to ur Long Variable Name
REgards
VeenaComment
-
I'm a bit behind on reading this thread. But is it possible that the problems are due to trying to convert a "system colour" to RGB? I remember having problems in VB6 because there are values which are more than &HFFFFFF, and thus cause an error when you try to split them into R, G and B components. That's because they are the "virtual" colours such as "button face" or "window background". They have to be translated to an actual colour value before you can do anything with it.Comment
-
Originally posted by Killer42I'm a bit behind on reading this thread. But is it possible that the problems are due to trying to convert a "system colour" to RGB?
QVeen's shown a nice shortcut to converting them to a 'universal form' (i.e. Hex), which can then be converted to 0 -> 255 (aka Byte)Comment
-
Well, I've now noticed that Veena's code to retrieve pixel-information always returns the same value, which is again a bug or something renewed. I think I better finish my java-course fast, or find a way to have an older version of Visual Studio for free. If anyone wants to try to create a function or something that gets pixel-info and converts it to RGB, and that works with Visual Studio 2008 bèta 2, please still post it, because I have a long way to go before I can program at this level in Java.
Yarr Of DoomComment
Comment