|
-
Dec 15th, 2011, 01:59 PM
#1
Thread Starter
Junior Member
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)?
-
Dec 15th, 2011, 02:06 PM
#2
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
-
Dec 15th, 2011, 03:59 PM
#3
Thread Starter
Junior Member
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.
-
Dec 16th, 2011, 04:56 AM
#4
Banned
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
-
Dec 16th, 2011, 05:01 AM
#5
Banned
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|