Monday, April 23, 2007

Colors in GDI+

One of my most favorite for creating component is playing with colors. With colors, we can create fancy controls for our application and present an "eye candy" application for the user. In this articles, you will find a way to playing with color from using simple basic color to creating advanced gradient color.

Part I: The Brush Object

To make a color, we need a Brush object from Graphics. There are 4 type of brush:

  1. Solid Brush : A plain color
  2. Hatch Brush : A color with hatch effect
  3. Texture Brush : A texture filled object
  4. Gradient Brush : A gradient color brush

Part II: Simple brush

To apply a color to your drawing, you need a graphic object to draw. It can be a pen or a shape.

Ex:


GDI+ has so many objects for displaying. They are:

  • Ellipses
  • Rectangles
  • Arcs (Arc is a portion of ellipse)
  • Beziers
  • Curves
  • Region
  • Path
  • Etc.

Conclusion

GDI+ is a good way for creating nice application UI, components or controls. With learning frequently, you can create any component you want. Your only limit is your creativity. In next section, I'll explain more complex way for manipulating colors using gradients.

Introduction to GDI+

GDI+ is a managed class from .NET Framework that provides function for drawing two-dimensional vector graphics, imaging, and typography. GDI stands for Graphical Device Interfaces and include in earlier Windows platform. GDI+ itself is the portion of Windows XP operating system and adding new features and optimized existing features.

GDI+ is one of most important things that you need to know when you're developing Windows Forms Application in .NET Framework, especially when you want to design a rich UI application, creating a customized component or user controls.

The Basics

GDI+ contains of 3 sections which are:

  1. Vector graphics (Lines, Curves and Shapes)
  2. Images, Bitmaps and Metafiles
  3. Coordinates and Transformations

    Note to Visual Basic 6.0 Users: You probably mentioned why there are no Line and Shape in toolbox area. That's because .NET Framework has replaced them with GDI+ function. You have to use GDI+ to drawing line and shape within the code.

GDI+ is a set of calls or procedure. So, in order to use it, you have to call those functions and passing the required parameters. Since you call a function, its effect can only be seen on runtime mode. You can't see them in design mode unlike VB6. Because you can't see anything in design mode, you have to input parameter and placed the coordinates precisely. That's a bit hard at start, but once you've mastered it, it will become very fun to use.

Using GDI+

To use GDI+, you will need Graphics object. Graphics is a managed interface for providing GDI+ functions. GDI+ class contains of these namespace:

  • System.Drawing
  • System.Drawing.Drawing2D
  • System.Drawing.Imaging
  • System.Drawing.Text
  • System.Drawing.Printing

For using them, there are 2 ways which are: Object.CreateGraphics, or in the Paint event. The Paint event is consider a safer way to using graphics, since the graphics object itself consume a big memory resource, especially if you work wit a lot graphics object. Besides, my articles will mostly use Paint event as well.

Openings

Welcome to Visual Basic 2005 Corner.

In this site, you will find articles about Visual Basic 2005. The topic can be vary from programming, .NET technology review, Tip and tricks and so on. The main reason for me to create this site is because I want to share my knowledge with others, especially who is work with Visual Basic 2005.

Finally, I hope you can find something usefull in this site and you can improved your skills after read articles in this site. Happy reading.

Regards

Michael Rawi