Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Obtaining the color of a given pixel on the screen

8 views
Skip to first unread message

Smartbridge Update

unread,
Oct 24, 2008, 7:57:01 PM10/24/08
to
I am unable to obtain the color of a given pixel on the screen. Back in the
days of QBasic, the following code returned the color value of a pixel at
(x,y).

color = point(x,y)

How can this be done in VB.NET?

Phillips@discussions.microsoft.com Ray Phillips

unread,
Dec 27, 2008, 3:13:01 AM12/27/08
to
Are you trying to get the pixel color of a point on the screen or a point on
an image?

To get the color at a certain point of an image:

dim myBitmap as bitmap = new bitmap("C:\test.bmp")
dim myColor as color
dim R,G,B as integer


'Gets the color of the top left pixel in the image
myColor = myBitmap.GetPixel(0,0)

R = myColor.R 'Gets the RGB red value
G = myColor.G 'Gets the RGB green value
B = myColor.B 'Gets the RGB blue value

'Conversely, you can use SetPixel() to change a pixels color at a given point:
R = 128
G = 0
B = 128

myColor = Color.FromARGB(255, R,G,B) 'The first value ("A") is "alpha"
value, or opacity.


If you want to get the color of a certain pixel on the screen, you can use a
similar method once a capture of the screen has been made:

'make a new bitmap to hold our image
Dim myBitmap As Bitmap

'make a color to hold our pixel's color
Dim myColor As Color

'Hide our form so it doesn't show up in the image
Me.Hide()

'Allow any events to finish before continuing
'Insures that our form is completely hidden before takeing the
screen capture
Application.DoEvents()

'Use sendkeys to send the "Print Screen" key.
'This is just like if you hit "Print Screen" on your keyboard.
SendKeys.Send("{PRTSC}")

'Copy the image from the clipboard to the bitmap we made earlier.
mybitmap = Clipboard.GetImage

'Show our form again.
Me.Show()

'Now we can use GetPixel just like before:
'Get the color of the pixel located at X=100, y=100.
myColor = myBitmap.GetPixel(100, 100)

'Set the title of our form to the RGB value of the pixel's color
Me.Text = myColor.R & ", " & myColor.G & ", " & myColor.B

expvb

unread,
Dec 27, 2008, 3:05:48 PM12/27/08
to
"Smartbridge Update" <Smartbri...@discussions.microsoft.com> wrote in
message news:2DA5BB91-3468-42CC...@microsoft.com...

> How can this be done in VB.NET?

Please post your question here:

news://microsoft.public.dotnet.languages.vb

This newsgroup is for VB6(VB Classic) and lower, which is incompatible with
what you are using. DotNet has separate groups; all of which have either
"dotnet" or "vsnet" in the name. If the group has "vb" in the name without
"dotnet", then it's for VB6 and lower only.

Thank you


0 new messages