mattscotney Posted July 10, 2003 Posted July 10, 2003 (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 July 10, 2003 by mattscotney Quote
ballisticnylon Posted July 10, 2003 Posted July 10, 2003 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. Quote "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
mattscotney Posted July 11, 2003 Author Posted July 11, 2003 Anywhere on the whole screen. Doing it for the client area would be a start though. Quote
*Gurus* divil Posted July 11, 2003 *Gurus* Posted July 11, 2003 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. Quote MVP, Visual Developer - .NET Now you see why evil will always triumph - because good is dumb. My free .NET Windows Forms Controls and Articles
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.