Click here to Skip to main content
15,915,867 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I would like my application to pick a specific pixel on the screen (not on my app) and then display the colour.
I'm a vb6 programmer, so if someone can give me step-by-step instructions I would greatly appreciate it.
Posted
Updated 8-Mar-12 7:08am
v2
Comments
Sergey Alexandrovich Kryukov 3-Mar-12 13:57pm    
It depends. Tag it: WPF, System.Drawing, anything else?
--SA
Jozi68 3-Mar-12 14:48pm    
Thanx for the reply.
What does tag it mean?
What is WPF?
Where does the system.drawing command go?
When I said set-by-step, I meant step-by-step
Sergey Alexandrovich Kryukov 3-Mar-12 16:54pm    
If I ask WPF or System.Drawing, I mean it.

This is not a place here to give you introduction of any of them. If you do any kind of UI, you usually use one or another (also could be Silverlight, similar to WPF but on the Web, or ASP.NET which has nothing to do with the topic, but could use System.Drawing internally on server side). Otherwise, you could use some 3rd-party library, but I would assume in this case you would specify it.

Do you do any UI at all? The answer would be based on that. If you did, you would know at least one of the libraries. If you did not start with either, you need to make a decision and then ask a question. The answers are very different for different types of applications and UI or imagine libraries. I don't want to give an answers for all possible options; this effort could be useless.

Alternatively, if you have no idea in which way you want to develop UI, you could ask an advice, but then you need to explain your purpose in detail, and perhaps tell use about your skills and experience. How else could we advice which decision to make?

Also, it is important how many pixels do you need. If it's just a few, GetPixel/SetPixel would do the trick, but for massive operations that would be prohibitively slow. Right approach, again, depends on what you are using for imagine.

--SA
Sergey Alexandrovich Kryukov 3-Mar-12 16:56pm    
Yes, a tag. Click "Improve question" to see: Tags. You can add "Forms", "System.Drawing" or "WPF".
--SA

1 solution

What does tag it mean?
Set the appropriate tags on your question: when you enter your question or edit it with "Improve Question" the Tags are just below the subject. They are a short-cut to classify your question so we can filter out things we know nothing about.

What is WPF?
Windows Presentation Foundation[^]

Where does the system.drawing command go?
Nowhere, it isn't a command but an assembly - it contains drawing code you can use if you reference the assembly in your application/


Try:

VB
<DllImport("gdi32")> _
Public Shared Function GetPixel(hDC As IntPtr, XPos As Integer, YPos As Integer) As UInteger
End Function

<DllImport("user32.dll", CharSet := CharSet.Auto)> _
Public Shared Function GetCursorPos(ByRef pt As POINT) As Boolean
End Function

<DllImport("User32.dll", CharSet := CharSet.Auto)> _
Public Shared Function GetWindowDC(hWnd As IntPtr) As IntPtr
End Function

''' <summary> 
''' Gets the System.Drawing.Color from under the mouse cursor. 
''' </summary> 
''' <returns>The color value.</returns> 
Public Shared Function [Get]() As Color
	Dim dc As IntPtr = GetWindowDC(IntPtr.Zero)

	Dim p As POINT
	GetCursorPos(p)

	Dim color__1 As Long = GetPixel(dc, p.X, p.Y)
	Return Color.FromArgb(CInt(color__1))
End Function


<StructLayout(LayoutKind.Sequential)> _
Public Structure POINT
	Public X As Integer
	Public Y As Integer
	Public Sub New(x__1 As Integer, y__2 As Integer)
		X = x__1
		Y = y__2
	End Sub
End Structure

(That is auto converted from my C# code, so it may or may not compile. Should do though, developer Fusion is pretty good)
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 3-Mar-12 16:58pm    
Griff, you probably know that GetPixel/SetPixel would be prohibitively slow in most cases. Also, P/Invoke would not be needed. I suggest we always wait until OP is clear about what UI or imagine library to use. I explained it all in my comments to the question.
--SA
Jozi68 8-Mar-12 13:05pm    
Thank you OriginalGriff. It works if I click on the form; it returns the background color of the form.
What I actually need is to supply the x,y coordinates, and then get the color. This must be ouside the application, e.g on a photo that is open in Paint.
Jozi68 8-Mar-12 13:46pm    
15 years ago I would have written a small DLL that accepts x,y coordinates, and returns the color of whatever it finds at that position. Then I simply would have called this DLL from my VB6 application.
Today I don't know where to start...
OriginalGriff 8-Mar-12 14:09pm    
You still can... :laugh:
GetCursorPos returns the cursor position in screen coordinates, not form - so it works for mouse positions outside the form as well as inside.
Try this: Set up a Timer, with an Interval of 50, and handle the Tick event. Start the timer.
In the Tick handler, set a label text to the Color returned by Get:
myLabel.Text = [Get]().ToString()
Run your app, and move the mouse around the screen.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900