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

Eyes

By , 11 Sep 2008
 

Sample Image

Introduction

This article demonstrates how to create a perfect, useless control using C# WinForms and .NET 2.0. The control uses pure managed code, and all visual elements can be adjusted through its properties.

Background

Is it not all developer's deepest desire to create something living? Well, not me! This was solely to have fun playing with the graphics. So, don't expect to see a mouth, nose, or brains in the future.

The Graphics

The process of building the eye.

Above are the basic elements of the eye: background, iris, shadow, pupil, reflex, and the eye-lid. The eye is drawn in this order.

Architecture

The Eye is a single class that inherits from the System.Windows.Forms.Control. Below are the properties and methods of the Eye class.

The Eye class

Using the Code

Using the Eye class is straightforward, but some properties may need some explanation.

  • BlinkStep is the rate in which the eye will close when blinking.
  • FocusPoint is the point the eye will look, in screen coordinates.
  • FocusAngle and FocusDistance are derived (read only) from FocusPoint.
  • LidOffset is used when TypeOfEye is Left or Right.

    The offset in pixels is the width of the iris divided by the value. Minimum is 3.

  • SlitSize is a percentage of the eye height.
  • TypeOfEye is an enum, and can take the values Left, Right, and Cyclops (default).
  • Blink() starts a new thread that will blink the eye.

The detailed iris is done using a color blend:

using (var path = new GraphicsPath())
{
  path.AddEllipse(rect);
  using (var gradientBrush = new PathGradientBrush(path))
  {
    gradientBrush.CenterPoint = centerPoint;
    var cb = new ColorBlend(4)
               {
                 Colors = irisColors,
                 Positions = new[] { 0.0F, 0.05F, 0.1F, 1.0F }
               };
    if (cb.Colors == null) return;
    gradientBrush.InterpolationColors = cb;
    g.FillPath(gradientBrush, path);
  }
}

Points of Interest

I attempted to squeeze the iris rectangle to make it more eye-like when going to the edge, but it never quite looked right.

History and Credits

This is the first version - 1.0.

License

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

About the Author

Niel M.Thomas
Software Developer (Senior)
Denmark Denmark
Member
Name: Niel Morgan Thomas
Born: 1970 in Denmark
Education:
Dataengineer from Odense Technical University.
More than 15 years in IT-business.
Current employment:
Working with application development in a major Danish company that produce medical equipment.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
GeneralNice idea and well done!memberMartin#7 Apr '09 - 1:45 
Hello Niel!
 
This was a very nice idea and well implemented!
Looking threw your code was really fine.
Your "eye" class is well structured and easy to understand! Good work!
 
I had just a short look but the only thing I could find which could be called a mistake, is the DefaultValue of the "FocusPoint" property.
[DefaultValue(typeof(Point), "{0;0}")]
should be
[DefaultValue(typeof(Point), "0,0")]
 
Thank you for taking time writing this code!
 
All the best,
 
Martin

GeneralToo Cool!memberArchKaine17 Sep '08 - 13:25 
"Eye" am going to have a ton of fun with this one. Thanks for the fun code. Big Grin | :-D
 
ArchKaine
 
Some say that ignorance is bliss... Blissful, aren't they?

GeneralAbth ProjectmemberYildirim Kocdag15 Sep '08 - 20:16 
Good job.
GeneralEyesmemberGreg.Farquhar15 Sep '08 - 20:10 
Really very nice
GeneralToo late (grin)memberfredsparkle15 Sep '08 - 6:56 
Heck code freeze is today, we have a diagnostic trace function in our product; so this would be a fun visual indicator to indicate that their data is being recorded.
QuestionProblems with sourcememberrht34110 Sep '08 - 7:13 
I'm having all kinds a difficulty with the source (trying to load in VS 2005):
 
-- First it won't load the project file at all, says "Microsoft.CSharp.targets project is missing".
-- If i remove that, the project will load, but:
o All references have errors.
o Can't build the project, "Build event does not exist".
 
I tried to download it and start over, but had the same results.
 
Thanks!
AnswerRe: Problems with source [modified]memberNiel M.Thomas10 Sep '08 - 8:16 
> Originally the article specified Visual Studio 2008 but it somehow it got lost in translation.
> Some declarations are specific to VS 2008 but you should be able to get it to work with VS 2005.
 
Now both (VS2005 & VS2008) sources are available.
 
[With best regards ]
[Niel M. Thomas ]
 

modified on Friday, September 12, 2008 1:10 AM

GeneralReally cool demo!memberJohnny J.9 Sep '08 - 23:00 
But I too would like to know what use you can make of this control... Smile | :)
 
/Johnny J.
JokeRe: Really cool demo!membermarkkuk9 Sep '08 - 23:16 
You could implement a .NET version of the classic Xeyes[^] application Smile | :)
GeneralGreat Project !!!memberRin Reaksmey9 Sep '08 - 16:07 
Thanks Mr. Thomas for this great project, but could you tell me what exactly the main reason for this control. It's just for fun or knowledge.
 
But I really appreciate this kind of work, thanks.
Generalpersonally...memberScott Bruno9 Sep '08 - 11:19 
I support *all* useless controls. Perhaps I'll embed an eye or two in some of these otherwise dull financial apps.
 
--
Saving the world, one line of code at a time

GeneralRe: personally...memberIlíon9 Sep '08 - 23:48 
I tend to agree with the underlying sentiment. But, at the same time, I recognize that such an exercise *can* be a learning tool. In which case it was not useless.
 
For instance, I may try to find the time to study the code, in hopes that I can learn something from it.
GeneralRe: personally...memberScott Bruno10 Sep '08 - 2:24 
I think maybe you didn't read the article very closely, and/or completely missed both the point and the spirit of what I wrote. Shrug.
 
--
Saving the world, one line of code at a time

GeneralRe: personally...memberIlíon10 Sep '08 - 6:23 
Scott Bruno wrote:
... and/or completely missed both the point and the spirit of what I wrote. Shrug.

I think not. *Shrug*
GeneralRe: personally...memberScott Bruno10 Sep '08 - 8:54 
First line in the article:
"This article demonstrates how to create a perfect, useless control using C# WinForms and .NET 2.0"
 
If you have any further questions... well, just keep 'em to yourself. As long as the author knows I was working with his own joke, I really don't care what Random Poster #11 has to say.
 
--
Saving the world, one line of code at a time

GeneralRe: personally...memberIlíon10 Sep '08 - 12:22 
Scott Bruno wrote:
As long as the author knows I was working with his own joke, I really don't care what Random Poster #11 has to say.

And yet you couldn't resist the temptation to so inform "Random Poster #11"
 
Scott Bruno wrote:
If you have any further questions... well, just keep 'em to yourself.

I think you've done a fine job of answering most any question about you.
GeneralRe: personally...membersinklink13 Sep '08 - 19:15 
Troll. The author even says he thinks the control is useless. Learn to read and not be a dick.
 
Thomas Hoggard

GeneralRe: personally...memberIlíon14 Sep '08 - 8:15 
Take your own advice, fool.
GeneralRe: personally...memberMarkRedman15 Sep '08 - 7:21 
If you're looking for an eye control, this is perfect!
 
Not that I need one, but if you controlled the expression in real-time it could certainly be used for an animated character in a game to show emotion.
 
...or for useful things like the office assistant Smile | :)
GeneralRe: personally...memberhackman3vilGuy15 Sep '08 - 9:21 
this control is good for people without eyes
GeneralRe: personally...memberLeblanc Meneses8 Nov '08 - 20:53 
lol.. i don't think they would be able to find this control.

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

Permalink | Advertise | Privacy | Mobile
Web02 | 2.6.130516.1 | Last Updated 11 Sep 2008
Article Copyright 2008 by Niel M.Thomas
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid