Click here to Skip to main content
6,822,123 members and growing! (19,041 online)
Email Password   helpLost your password?
Desktop Development » Miscellaneous » Miscellaneous Controls     Intermediate License: The Code Project Open License (CPOL)

Eyes

By Niel M.Thomas

Creating an eye control.
C# (C#2.0), Windows, .NET (.NET2.0), GDI+, Dev
Posted:9 Sep 2008
Updated:11 Sep 2008
Views:17,928
Bookmarked:45 times
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
28 votes for this article.
Popularity: 6.92 Rating: 4.78 out of 5
1 vote, 3.6%
1

2
1 vote, 3.6%
3
1 vote, 3.6%
4
25 votes, 89.3%
5

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


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

Other popular Miscellaneous articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 21 of 21 (Total in Forum: 21) (Refresh)FirstPrevNext
GeneralNice idea and well done! PinmemberMartin#2:45 7 Apr '09  
GeneralToo Cool! PinmemberArchKaine14:25 17 Sep '08  
GeneralAbth Project PinmemberYildirim Kocdag21:16 15 Sep '08  
GeneralEyes PinmemberGreg.Farquhar21:10 15 Sep '08  
GeneralToo late (grin) Pinmemberfredsparkle7:56 15 Sep '08  
QuestionProblems with source Pinmemberrht3418:13 10 Sep '08  
AnswerRe: Problems with source [modified] PinmemberNiel M.Thomas9:16 10 Sep '08  
GeneralReally cool demo! PinmemberJohnny J.0:00 10 Sep '08  
JokeRe: Really cool demo! Pinmembermarkkuk0:16 10 Sep '08  
GeneralGreat Project !!! PinmemberRin Reaksmey17:07 9 Sep '08  
Generalpersonally... PinmemberScott Bruno12:19 9 Sep '08  
GeneralRe: personally... PinmemberIlíon0:48 10 Sep '08  
GeneralRe: personally... PinmemberScott Bruno3:24 10 Sep '08  
GeneralRe: personally... PinmemberIlíon7:23 10 Sep '08  
GeneralRe: personally... PinmemberScott Bruno9:54 10 Sep '08  
GeneralRe: personally... PinmemberIlíon13:22 10 Sep '08  
GeneralRe: personally... Pinmembersinklink20:15 13 Sep '08  
GeneralRe: personally... PinmemberIlíon9:15 14 Sep '08  
GeneralRe: personally... PinmemberMarkRedman8:21 15 Sep '08  
GeneralRe: personally... Pinmemberhackman3vilGuy10:21 15 Sep '08  
GeneralRe: personally... PinmemberLeblanc Meneses21:53 8 Nov '08  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

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

PermaLink | Privacy | Terms of Use
Last Updated: 11 Sep 2008
Editor: Sean Ewington
Copyright 2008 by Niel M.Thomas
Everything else Copyright © CodeProject, 1999-2010
Web21 | Advertise on the Code Project