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
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#.