Click here to Skip to main content
15,120,883 members
Articles / Programming Languages / C#
Posted 7 Aug 2007


26 bookmarked

Selecting Colors from Anywhere on the Screen

Rate me:
Please Sign up or sign in to vote.
3.71/5 (6 votes)
7 Aug 2007CPOL2 min read
Allows you to drag an eyedropper anywhere on the screen to select a color
Screenshot - Eyedropper.gif


Two weeks ago, I was a VB6 programmer of 10 years standing, confident of what I could do with the language and how to do it. Now, all of a sudden, I'm a newbie again, hip deep in learning C# 2.0. This little program is one of my initial efforts and may contain some code that will be of use to you.

The Eyedropper program displays a picture of a yellow dog. When you click on an eyedropper button and drag your mouse anywhere on the screen, the color of the dog changes to whatever color is under the cursor. To make this work, I had to learn how to:

  • capture the position of the mouse in screen coordinates
  • capture the entire screen as a bitmap
  • setup a color cursor from an embedded resource

The program itself is trivial, but these techniques are useful in many other applications and took some time to ferret out, so hopefully this little program will prove a time saver for you.


I always loved when my paint program allowed me to select a color from anywhere on the screen. I could do it in VB6, but I wondered how hard it would be in C#. Turns out it wasn't too bad due to the resources concerning C# that abound on the net.

Using the Code

The program is very simple, once you realize that part of the dog picture is transparent (loaded from a PNG) and that the backgroundcolor of the picturebox just shows through. All I had to do was change this to change the mutt's color. All the real magic is in the Utility class.

CreateColorCursorFromResourceFile loads a colored curser from an inbedded resource. This thing is wonderful. I spent most of a day trying to figure out how this could be done and finally, finally found the answer in an article at The Code Project written by Bingzhe Quan concerning his scrollable, zoomable and scalable picture box. I just copied his code. I almost understand it.

GetCursorPostionOnScreen gets the current cursor position in screen coordinates.

CaptureScreen copies the screen into a bitmap.

Points of Interest

Nothing's perfect and so it is with this little program. It seems like overkill to grab the entire screen into a bitmap just to find a simple little color, doesn't it? There must be a better way. Also, as you move the mouse rapidly across the screen, the dog picture flickers. I wish it didn't.

As mentioned above, I am new to C# and Visual Studio 2005, so if I have strayed from the norms in my use of the language or environment, I would appreciate some feedback.


  • 8/7/2007: Initial version of the article


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


About the Author

Web Developer
United States United States
I have been at this for a long time now. Back in school I learned FORTRAN. Around 1970 I was hired by Burroughs Corp (ALGOL, ESPOL.) After that I worked at the University of Delaware (APL, Pascal) and at Clarion University of Pennsylvania (TRS-80 and Apple BASIC.) In 1986 I started Hufnagel Software and today publish a CAD package for event design called PartyCAD (VB6.) My latest enthusiasm though is learning C#.

Comments and Discussions

GeneralCursor Creation Pin
Johannes Wallroth13-Aug-07 21:45
MemberJohannes Wallroth13-Aug-07 21:45 
GeneralRe: Cursor Creation Pin
hankhuf14-Aug-07 6:25
Memberhankhuf14-Aug-07 6:25 
GeneralRe: Cursor Creation Pin
Johannes Wallroth14-Aug-07 6:54
MemberJohannes Wallroth14-Aug-07 6:54 
GeneralRe: Cursor Creation Pin
Johannes Wallroth14-Aug-07 7:36
MemberJohannes Wallroth14-Aug-07 7:36 
GeneralRe: Cursor Creation Pin
hankhuf14-Aug-07 12:50
Memberhankhuf14-Aug-07 12:50 
So close, so close:

Cursor cursor;<br />
Bitmap bitmap = new Bitmap(32,32);<br />
Graphics g = Graphics.FromImage(bitmap);<br />
g.DrawImage(Properties.Resources.RedEye,0,0);<br />
IntPtr ptr = bitmap.GetHicon();<br />
cursor = new Cursor(ptr);<br />
cursor.HotSpot = new Point(2, 25);<br />
return cursor;

The above works if you comment out the setting of the hotspot, which is a readonly property. I could interrogate the hotspot and adjust the point I look at on the bitmap accordingly, but that is a bit of a kludge. Any ideas?


GeneralRe: Cursor Creation Pin
Johannes Wallroth15-Aug-07 10:39
MemberJohannes Wallroth15-Aug-07 10:39 
GeneralRe: Cursor Creation Pin
hankhuf15-Aug-07 11:14
Memberhankhuf15-Aug-07 11:14 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.