Dropper color tool

mattscotney

Member
Joined
Jul 9, 2003
Messages
13
How do I create an color dropper tool?

I need the code to get the color of the pixel at the position of the mouse.

This is what I have done so far, I can get the pixel color of a bitmap, but how do I get the color of the screen at any given pixel?
Code:
Dim bmp = New Bitmap("bg.gif")
Dim z As Color

z = bmp.GetPixel(50, 50)
MsgBox(z.ToString)

Label4.BackColor = z

Thanks in advance
 
Last edited by a moderator:
Do you mean anywhere in the client area, or anywhere on the whole screen? Because receiving mouse events from outside is a bit tricky, and requires API calls, I think.
 
Code:
    Private Declare Function GetPixel Lib "gdi32" Alias "GetPixel" (ByVal hdc As IntPtr, ByVal x As Integer, ByVal y As Integer) As Integer
    Private Declare Function GetDC Lib "user32" Alias "GetDC" (ByVal hwnd As IntPtr) As IntPtr
    Private Declare Function ReleaseDC Lib "user32" Alias "ReleaseDC" (ByVal hwnd As IntPtr, ByVal hdc As IntPtr) As Integer

    Private Sub Panel1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Panel1.MouseMove
        Dim hdc As IntPtr = GetDC(IntPtr.Zero)
        Dim p As Point = Panel1.PointToScreen(New Point(e.X, e.Y))
        Dim c As Integer = GetPixel(hdc, p.X, p.Y)

        Dim color As Color = ColorTranslator.FromOle(c)
        ReleaseDC(IntPtr.Zero, hdc)

        Panel1.BackColor = color
    End Sub

Thatll do it. Click on the panel and you can drag over the whole screen, the panels background colour will change as you move over different coloured pixels.
 
Back
Top