Click here to Skip to main content
Click here to Skip to main content

Selecting Colors from Anywhere on the Screen

By , 7 Aug 2007
Rate this:
Please Sign up or sign in to vote.
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)

About the Author

hankhuf
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 PinmemberJohannes Wallroth13-Aug-07 20:45 
GeneralRe: Cursor Creation Pinmemberhankhuf14-Aug-07 5:25 
GeneralRe: Cursor Creation PinmemberJohannes Wallroth14-Aug-07 5:54 
GeneralRe: Cursor Creation PinmemberJohannes Wallroth14-Aug-07 6:36 
GeneralRe: Cursor Creation Pinmemberhankhuf14-Aug-07 11:50 
GeneralRe: Cursor Creation PinmemberJohannes Wallroth15-Aug-07 9:39 
GeneralRe: Cursor Creation Pinmemberhankhuf15-Aug-07 10:14 

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

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

| Advertise | Privacy | Mobile
Web03 | 2.8.140415.2 | Last Updated 7 Aug 2007
Article Copyright 2007 by hankhuf
Everything else Copyright © CodeProject, 1999-2014
Terms of Use
Layout: fixed | fluid