Jump to content
Xtreme .Net Talk

Recommended Posts

Posted (edited)

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?

Dim bmp = New Bitmap("bg.gif")
Dim z As Color

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

Label4.BackColor = z

 

Thanks in advance

Edited by mattscotney
Posted
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.

"It may be roundly asserted that human ingenuity cannot concoct a cipher which human ingenuity cannot resolve."

 

- Edgar Allan Poe, 1841

 

I long to accomplish great and noble tasks, but it is my chief duty to accomplish humble tasks as though they were great and noble. The world is moved along, not only by the mighty shoves of its heroes, but also by the aggregate of the tiny pushes of each honest worker.

 

- Helen Keller

  • *Gurus*
Posted

   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

 

That'll do it. Click on the panel and you can drag over the whole screen, the panel's background colour will change as you move over different coloured pixels.

MVP, Visual Developer - .NET

 

Now you see why evil will always triumph - because good is dumb.

 

My free .NET Windows Forms Controls and Articles

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...