Click here to Skip to main content
15,878,959 members
Articles / Programming Languages / C#
Article

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 32.3K   1.2K   26   7
Allows you to drag an eyedropper anywhere on the screen to select a color
Screenshot - Eyedropper.gif

Introduction

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.

Background

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.

History

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

License

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


Written By
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 20:45
Johannes Wallroth13-Aug-07 20:45 
GeneralRe: Cursor Creation Pin
hankhuf14-Aug-07 5:25
hankhuf14-Aug-07 5:25 
GeneralRe: Cursor Creation Pin
Johannes Wallroth14-Aug-07 5:54
Johannes Wallroth14-Aug-07 5:54 
GeneralRe: Cursor Creation Pin
Johannes Wallroth14-Aug-07 6:36
Johannes Wallroth14-Aug-07 6:36 
GeneralRe: Cursor Creation Pin
hankhuf14-Aug-07 11:50
hankhuf14-Aug-07 11:50 
GeneralRe: Cursor Creation Pin
Johannes Wallroth15-Aug-07 9:39
Johannes Wallroth15-Aug-07 9:39 
GeneralRe: Cursor Creation Pin
hankhuf15-Aug-07 10:14
hankhuf15-Aug-07 10: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.