Results 1 to 5 of 5

Thread: Pixel color at cursor position

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Nov 2011
    Posts
    17

    Question Pixel color at cursor position

    In Visual Basic 2010, how do I fetch the color of the pixel on the cursor's position (whether it be outside the program or inside)?

  2. #2
    Master Of Orion ForumAccount's Avatar
    Join Date
    Jan 2009
    Location
    Canada
    Posts
    2,802

    Re: Pixel color at cursor position

    I just Googled your thread name + VB.NET at the end and got this:
    Code:
    Public NotInheritable Class Win32
    
        '//apis
        <DllImport("user32.dll")> _
        Private Shared Function GetDC(ByVal hwnd As IntPtr) As IntPtr
        End Function
    
        <DllImport("user32.dll")> _
        Private Shared Function ReleaseDC(ByVal hwnd As IntPtr, _
                                          ByVal hdc As IntPtr) As Int32
        End Function
    
        <DllImport("gdi32.dll")> _
        Private Shared Function GetPixel(ByVal hdc As IntPtr, _
                                         ByVal nXPos As Integer, _
                                         ByVal nYPos As Integer) As UInteger
        End Function
    
        '//methods
        Public Shared Function GetPixelColor(ByVal x As Integer, _
                                             ByVal y As Integer) As System.Drawing.Color
            Dim hdc As IntPtr = Win32.GetDC(IntPtr.Zero)
            Dim pixel As UInteger = Win32.GetPixel(hdc, x, y)
            Win32.ReleaseDC(IntPtr.Zero, hdc)
            Return Color.FromArgb(CInt(pixel And &HFF), _
                                  CInt(pixel And &HFF00) >> 8, _
                                  CInt(pixel And &HFF0000) >> 16)
        End Function
    
    End Class

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Nov 2011
    Posts
    17

    Re: Pixel color at cursor position

    Well, I am using your code together with this to draw a zoomed in version of the screen on the form (8x8 pixels):
    Code:
    ...
    Dim xx, yy, ww, hh, mousex, mousey As Integer
    ww = 8
    hh = 8
    mousex = MousePosition.X
    mousey = MousePosition.Y
    For xx = -4 To 4
        For yy = -4 To 4
            Dim rect As New Rectangle(160 + xx * ww, 72 + yy * hh, ww, hh)
            draw.Color = Win32.GetPixelColor(mousex + xx, mousey + yy)
            preview.FillRectangle(draw, rect)
        Next
    Next
    I have put this in a timer that updates every 100 milliseconds. However, this is EXTREMELY slow, is there a way to optimize it? Even if I put the timer at 1 second, it still lags like hell.

    EDIT: Nevermind, reduced all the lag by only checking the pixels once.
    Last edited by Davve; Dec 15th, 2011 at 04:38 PM.

  4. #4
    Banned
    Join Date
    Mar 2009
    Posts
    764

    Re: Pixel color at cursor position

    41 screen capture
    Code:
    Public Class Form1
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Dim screenBounds = Screen.PrimaryScreen.Bounds
            Dim screenShot As New Bitmap(screenBounds.Width, screenBounds.Height)
            Using g = Graphics.FromImage(screenShot)
                g.CopyFromScreen(screenBounds.Location, Point.Empty, screenBounds.Size)
            End Using
            PictureBox1.Image = screenShot
            'Dim filePath = IO.Path.Combine(My.Computer.FileSystem.SpecialDirectories.MyPictures, "ScreenShot.bmp")
            'screenShot.Save(filePath, Imaging.ImageFormat.Bmp)
        End Sub
    End Class
    use the .GetPixelColor(mousex + xx, mousey + yy)
    on the image captured

  5. #5
    Banned
    Join Date
    Mar 2009
    Posts
    764

    Re: Pixel color at cursor position

    in the timer tick event :
    Code:
    Dim screenBounds = Screen.PrimaryScreen.Bounds
            Dim screenShot As New Bitmap(screenBounds.Width, screenBounds.Height)
            Using g = Graphics.FromImage(screenShot)
                g.CopyFromScreen(screenBounds.Location, Point.Empty, screenBounds.Size)
            End Using
            me.text = screenShot.GetPixelColor(cursor.position.x, cursor.position.y)
    haven't tasted this yet

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width