Question Getting pixelcolor from screen

groover

Member
Joined
Oct 10, 2009
Messages
23
Programming Experience
1-3
Hi all,

I am using the following code to get the color on the mouseposition:

Function GetScreenPixel(ByVal pt As Point) As Color
Using bmp As New Bitmap(1, 1)
Using g As Graphics = Graphics.FromImage(bmp)
g.CopyFromScreen(pt, Point.Empty, New Size(1, 1))
Return bmp.GetPixel(0, 0)
End Using
End Using
End Function

Private Sub Timer4_Elapsed(ByVal sender As System.Object, ByVal e As System.Timers.ElapsedEventArgs) Handles Timer4.Elapsed
col2 = GetScreenPixel(MousePosition)
TextBox8.Text = col2.ToString & MousePosition.ToString

End Sub

The results appear in the textbox, but not at the right coordinates .
The mouse position is OK, but he colors are only right at the left side of the screen.

How can I fix this?

Thanks in advance,

Groover
 
Hi,

As there is no reply to my question, I will add some extra information.
It seems to be that the colors that are picked are halfway the x and y cursor position.
I changed the function to the following:

Function GetScreenPixel(ByVal pt As Point) As Color
Dim screenSize As Size = My.Computer.Screen.Bounds.Size
Using bmp As New Bitmap(screenSize.Width, screenSize.Height)
Using g As Graphics = Graphics.FromImage(bmp)
g.CopyFromScreen(pt, Point.Empty, New Size(screenSize.Width, screenSize.Height))

Return bmp.GetPixel(0, 0)
End Using
End Using
End Function

The results are exactly the same.

Is there anyone who can help me?

thanks,

Groover
 
hi all,

I have found the cause of my problem!

Because I had trouble with reading the small fonts on my large HDMI screen I changed the dpi settings for my desktop to 192 dpi, not realizing that it would have effect on the bitmap size in relation to the real screen size.
Is there a way to make it work with my dpi settings, or make it work with all dpi settings?

An other question:
Would Windows 7 make a difference, I am now using Windows Vista and Microsoft Visual basic 2010 express Beta.

Thanks,

Groover
 
Merry Christmas everyone!

Still no solution for my problems?

I am trying a different approach, but still would like an answer to my previous questions.

I am trying to get the color from rectangleshapes in my form, because they are at the right screen coordinates, but the code I use is not working. At debugging it freezes my program.

I have this code called in a timer:

VB.NET:
Private Sub GetPixel_Example()
        Dim x As Long
        Dim y As Long
        x = Cursor.Position.X
        y = Cursor.Position.Y
        Dim myBitmap As New Bitmap(RectangleShape2.Width, RectangleShape2.Height)
        Dim rect As New Rectangle(0, 0, RectangleShape2.Width, RectangleShape2.Height)
        RectangleShape2.DrawToBitmap(myBitmap, rect)
        myBitmap.Save("test.jpg") 'Test if code is working to this point ,is OK
               Dim pixelColor As Color = myBitmap.GetPixel(x, y)
        TextBox8.Text = pixelColor.ToString & MousePosition.ToString

    End Sub
I cannot figure out what is wrong,

Please help,

groover
 
Back
Top