473,406 Members | 2,713 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Retrieving pixel information

YarrOfDoom
1,247 Expert 1GB
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.
Aug 31 '07 #1
14 2848
QVeen72
1,445 Expert 1GB
Hi,

Check this :

Expand|Select|Wrap|Line Numbers
  1. Public bmp As Bitmap 
  2.  
  3.   Dim Tcolour As New Color() 
  4.   Dim cR As Integer 
  5.   Dim cG As Integer 
  6.   Dim cB As Integer 
  7.  
  8.   Tcolour = bmp.GetPixel(x, y) 
  9.   cR = colour.R 
  10.   cG = colour.G 
  11.   cB = colour.B 
  12.  
  13.  
Regards
Veena
Aug 31 '07 #2
YarrOfDoom
1,247 Expert 1GB
I've tried this, but the when I try to run the program, I recieve the warning: "NullReferenceExeption was unhandled."Yarr Of Doom
Aug 31 '07 #3
YarrOfDoom
1,247 Expert 1GB
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
Sep 1 '07 #4
YarrOfDoom
1,247 Expert 1GB
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...
Sep 2 '07 #5
QVeen72
1,445 Expert 1GB
Hi,

Check this :

Expand|Select|Wrap|Line Numbers
  1. Private Declare Function CreateDCA Lib "gdi32" (ByVal lpDriverName As String, lpDeviceName As Any, lpOutput As Any, lpInitData As Any) As Long
  2. Private Declare Function DeleteDC Lib "gdi32" (ByVal hdc As Long) As Long
  3. Private Declare Function GetPixel Lib "gdi32" (ByVal hdc As Long, ByVal x As Long, ByVal y As Long) As Long
  4.  
  5. Public Function MyPxInfo(ByVal x As Integer, ByVal y As Integer) As Long
  6.     DeskTopDC = CreateDCA("DISPLAY", ByVal 0&, ByVal 0&, ByVal 0&)
  7.     MyPxInfo = GetPixel(DeskTopDC, x, y)
  8.     DeskTopDC = DeleteDC(DeskTopDC)
  9. End Function
  10.  
Call the function :
RGB = MyPxInfo(100,200)

Regards
Veena
Sep 2 '07 #6
YarrOfDoom
1,247 Expert 1GB
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
Sep 2 '07 #7
Robbie
180 100+
Make a new module and put this in it:
Expand|Select|Wrap|Line Numbers
  1. Const C256 As Long = 256
  2. Const C256Power2 As Long = (256 ^ 2)
  3.  
  4. 'Return variables for after converting colour number to separate RGB values:
  5. Global ReturnR As Long
  6. Global ReturnG As Long
  7. Global ReturnB As Long
  8.  
  9.  
  10. Public Sub ColourToRGB(ColourNumberToConvert As Long)
  11. 'WHEN THIS FUNCTION HAS FINISHED, THESE VARIABLES WILL
  12. 'GIVE YOU THE R, G AND B VALUES OF THE ORIGINAL COLOUR NUMBER:
  13. 'ReturnR - 0 -> 255 = Red value
  14. 'ReturnG - 0 -> 255 = Green value
  15. 'ReturnB - 0 -> 255 = Blue value
  16.  
  17.     Dim NumberLeftSoFar As Long
  18.     NumberLeftSoFar = ColourNumberToConvert
  19.  
  20.     ReturnB = Fix(NumberLeftSoFar / C256 / C256)
  21.     NumberLeftSoFar = NumberLeftSoFar - (ReturnB * C256Power2)
  22.     ReturnG = Fix(NumberLeftSoFar / C256)
  23.     ReturnR = NumberLeftSoFar - (ReturnG * C256)
  24.  
  25. End Sub
  26.  
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!
Sep 2 '07 #8
YarrOfDoom
1,247 Expert 1GB
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
Sep 2 '07 #9
Robbie
180 100+
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...
Sep 2 '07 #10
YarrOfDoom
1,247 Expert 1GB
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?
Sep 3 '07 #11
QVeen72
1,445 Expert 1GB
Hi,

Just Check this out :

First Convert Long To Hex and extract RGB:

Expand|Select|Wrap|Line Numbers
  1. Dim THex
  2. Dim NewHex
  3. Dim R, G, B
  4.  
  5. THex = Hex(LongColor)
  6. Dim i As Byte
  7.  
  8. On Error Resume Next
  9. THex = Right((THex), 6)
  10. NewHex =THex 
  11. For i = 1 To (6 - Len(THex))
  12.    NewHex = NexHex & "0"
  13. Next
  14. R = CByte("&H" & Right$(NewHex, 2))
  15. G = CByte("&H" & Mid$(NewHex, 3, 2))
  16. B = CByte("&H" & Left$(NewHex, 2))
  17.  
Just Change "LongColor" to ur Long Variable Name

REgards
Veena
Sep 3 '07 #12
Killer42
8,435 Expert 8TB
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.
Sep 3 '07 #13
Robbie
180 100+
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)
Sep 3 '07 #14
YarrOfDoom
1,247 Expert 1GB
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
Sep 3 '07 #15

Sign in to post your reply or Sign up for a free account.

Similar topics

1
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). ...
8
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...
2
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...
30
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...
1
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...
2
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...
4
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...
6
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...
7
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...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
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...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
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,...
0
Oralloy
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,...
0
jinu1996
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...
0
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...
0
tracyyun
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...
0
isladogs
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...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.