Click here to Skip to main content
16,012,061 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I need to determine the color of various points (pixels) on the form including graphic shapes (lines, circles, etc) that I add myself. This was quite easy in VB6 but seems to be very hard in vb.net 10.

I found the code snippet below on a google search. The CopyFromScreen function actually worked to copy a 1 pixel rectangle to point 0,0 and then read it's color. But, when I try to replace Windows.Forms.Cursor.Postion with actual X,Y coordinates like (10,10) or even (me.location.x, me.location.y) I get an error message stating that integers can be used with CopyFromScreen.

Any ideas on why I cannot replace Windows.Forms.Cursor.Postion with numbers or some type of declared variable???

As always, thanks in advance for any help!!
Gary Vogel


Public Class Form1

Private WithEvents timer1 As New Windows.Forms.Timer

Private Sub Form1_Load(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles MyBase.Load

timer1.Interval = 10
timer1.Start()

End Sub

Private Sub Timer1_Tick(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles timer1.Tick

Using bmp As New Bitmap(1, 1)
Using g As Graphics = Graphics.FromImage(bmp)
g.CopyFromScreen(Windows.Forms.Cursor.Position, _
New Point(0, 0), New Size(1, 1))
End Using
Me.Text = bmp.GetPixel(0, 0).ToString
Me.Invalidate()
End Using

End Sub

End Class
Posted

1 solution

Windows.Forms.Cursor.Position is of type Point and can be used to define your own point and pass that as parameter.

VB
System.Drawing.Point MyPosition = new Point(100, 100)
g.CopyFromScreen(MyPosition, New Point(0, 0), New Size(1, 1))


Good luck!
 
Share this answer
 
Comments
Sandeep Mewara 18-Nov-10 1:34am    
Comment from OP:
Thanks a bunch!!! Would have NEVER figured out that one on my own!!!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900