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.
14 2849
Hi,
Check this : -
Public bmp As Bitmap
-
-
Dim Tcolour As New Color()
-
Dim cR As Integer
-
Dim cG As Integer
-
Dim cB As Integer
-
-
Tcolour = bmp.GetPixel(x, y)
-
cR = colour.R
-
cG = colour.G
-
cB = colour.B
-
-
Regards
Veena
I've tried this, but the when I try to run the program, I recieve the warning: "NullReferenceExeption was unhandled."Yarr Of Doom
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 Doom
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...
Hi,
Check this : -
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("DISPLAY", ByVal 0&, ByVal 0&, ByVal 0&)
-
MyPxInfo = GetPixel(DeskTopDC, x, y)
-
DeskTopDC = DeleteDC(DeskTopDC)
-
End Function
-
Call the function :
RGB = MyPxInfo(100,200)
Regards
Veena
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 Doom
Make a new module and put this in it: -
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(ColourNumberToConvert 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 = ColourNumberToConvert
-
-
ReturnB = Fix(NumberLeftSoFar / C256 / C256)
-
NumberLeftSoFar = NumberLeftSoFar - (ReturnB * C256Power2)
-
ReturnG = Fix(NumberLeftSoFar / C256)
-
ReturnR = NumberLeftSoFar - (ReturnG * C256)
-
-
End Sub
-
Now you can run ColourToRGB(), then look at the 3 global variables ReturnR, ReturnG and ReturnB for the red, green and blue values, respectively. ColourNumberToConvert 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!
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 Doom
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 Doom
Hello. I have tested Veena's code, i.e. replaced "GetPixel()" code in one of my programs with the MyPxInfo(), and it works for me.
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...
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?
Hi,
Just Check this out :
First Convert Long To Hex and extract RGB: -
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))
-
Just Change "LongColor" to ur Long Variable Name
REgards
Veena
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.
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 think Yarr and QVeen have cracked it. The problem is, we're used to 24-bit colours but it seems VB 2008 gives back 64-bit colours with GetPixel().
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)
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 Doom
Sign in to post your reply or Sign up for a free account.
Similar topics
by: cobalt |
last post by:
Hi all. I know nothing about javascript, so anything you say will likely be
news to me. I have a vhost that may have a configuration problem (1 vhost
is getting way more traffic than it should). ...
|
by: Steve |
last post by:
Can anyone tell me the preferred method for writing and
retrieving persistent information using .Net.
Specifically, I am referring to information that you used
to see in registry keys or .ini...
|
by: Sakke |
last post by:
Hello!
We have written a GCryptoSvr.dll COM server in C++. Inside that resides
WebClient COM component. WebClient CLSID is
{8DC27D48-F94C-434B-A509-C3E1A3E75B9E}. When we are using that...
|
by: Chaos |
last post by:
As my first attempt to loop through every pixel of an image, I used
for thisY in range(0, thisHeight):
for thisX in range(0, thisWidth):
#Actions here for Pixel thisX, thisY
But it takes...
|
by: jimmyfo |
last post by:
Hi, I recently wrote an ASP.Net web application in VS2005 and published
(using VS2005 Publish feature) it to a relatively clean machine with
ASP.Net 2.0 and MDAC 2.8 installed on it. However, when...
|
by: ajay_itbhu |
last post by:
Hi everyone, I want to read the pixel values of 2 similar images in
bitmap format like 2 continuous frame of a video for calculating the
median. But i dont know how to read the pixel values of...
|
by: kaisersose1995 |
last post by:
Hi,
I'm trying to get Novell Netware information using vba into my access
database and have no
idea how to progress. Basically when i right click the properties on
my computer the free space of a...
|
by: 123shailesh |
last post by:
Hello everyone,
I need some help. I have been working on it for some time but havent been able to think of any solution. Even had thought of making do without it, even though it was a major part...
|
by: w1ck3d64 |
last post by:
hi,
i have an array of pixel information which i want to display with C/C++. I'm trying to use OpenCV at the moment but I don't have much experience writing pixel by pixel. Can someone show me a...
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new...
| |