Drawing Functions
In This Chapter
- Key Classes Related to Drawing
- Drawing with the .NET Namespaces
- Drawing Basics
- Drawing Basic Shapes
- Filling Shapes
- Collections of Shapes
- Working with Images
- Transformations
- Learning by Example: A Forms-Based Drawing Application
Nothing in Windows gets to the user's screen without the aid of a drawing function. This includes images, colors, and even text. The OS must render all things visually by drawing pixels to an output device (monitor, printer, and so on). Of course, Windows does a good job of hiding drawing functions from the average developer. When did you last need to call an API function to display text to the screen or change the background color of a button? Our controls, compiler, and operating system serve to limit our need to make direct calls into the drawing library. However, there is always the case where your application requirements are beyond the scope of what can be done with controls and so on. Perhaps you must create custom pie charts for your users on-the-fly. Or maybe you need to allow your users to view a group of fonts or send output to the printer. Chances are that you will eventually need to write your own custom visual display code. This is where the .NET drawing library comes into play. It provides you with a host of classes that make adding drawing capabilities to your application easy and fun.
This chapter illustrates common programming tasks using the namespaces related to drawing in the .NET Framework Class Library. The chapter starts by illustrating the key classes used to execute drawing functions with the namespace. Then follows a detailed discussion of these key classes and related code examples. Lastly, we will create a simple drawing application that serves to demonstrate how these classes can be used in the context of a larger application and serves as an experimental ground.
After reading this chapter, you should be able to do the following:
-
Understand how Windows manages coordinates
-
Draw basic shapes including lines, curves, rectangles, and polygons
-
Fill shapes and lines with various colors, patterns, and gradients
-
Work with groups of shapes
-
Work with bitmaps and icons in your application
-
Rotate, stretch, and skew graphics
Key Classes Related to Drawing
The process of drawing with the .NET Framework Class Library involves a whole host of classes. These classes can be found inside of the System.Drawing namespace and its associated third-level namespaces. At a glance, the drawing namespace in .NET is made up of the following:
System.Drawing: Provides basic graphics functionality. This chapter focuses on this namespace.
System.Drawing.Design: Focuses on providing functionality for extending the design time environment. This namespace is beyond the scope of this book.
System.Drawing.Drawing2D: Provides two-dimensional and vector graphics classes and methods. This namespace is covered within this chapter.
System.Drawing.Imaging: Exposes advanced imaging functionality. This namespace is beyond the scope of this book.
System.Drawing.Printing: Gives you classes to manage output to a print device. Chapter 6, "Font, Text, and Printing Operations," covers this namespace.
System.Drawing.Text: Wraps fonts and type management. Chapter 6 covers this namespace.
This chapter is focused on the System.Drawing and System.Drawing.Drawing2D namespaces. These two namespaces contain classes that are fundamental to the execution of common programming tasks with .NET. The namespaces Printing and Text are covered elsewhere in the book, and Design and Imaging are simply beyond the scope of this book as they encapsulate more specialized features. There are certainly great classes within these namespaces, and we encourage you to use this chapter as a leaping-off point to your own exploring. Table 9.1 lists the key classes we will be discussing.
Table 9.1 Key Classes of System.Drawing and System.Drawing.Drawing2D
Class |
Description |
Functional Drawing Classes |
|
Graphics |
The Graphics class is the premier class within the namespace for executing drawing and filling shapes. |
GraphicsState |
The GraphicsState class is used to save the state of the Graphics object between calls to transformations and the like. This class is used with the BeginContainer and EndContainer methods of the Graphics class. |
Drawing Basics |
|
Pen |
The Pen class is used to draw the outlines of objects (lines, rectangles, ellipses, and so on). It defines the line weight and color similar to an actual pen. |
Rectangle |
The Rectangle structure stores information about a rectangle (location, width, and height). This structure is used to draw rectangles, ellipses, pies, and so on. |
CustomLineCap |
The CustomLineCap class is used to create a custom, user-defined end cap for a line. |
Working with Images |
|
Bitmap |
The Bitmap class encapsulates an image of type bitmap. |
Icon |
The Icon class encapsulates a small bitmap image used to represent an object. |
Image |
The Image class is the abstract base class used for both the Bitmap and the Icon classes. |
Graphic Fills |
|
Brush |
The abstract Brush class is the base class for the various brush classes throughout the drawing namespace. Brushes are used to fill shapes with colors, textures, and patterns. |
SolidBrush |
The SolidBrush class is a brush made of one solid color. |
TextureBrush |
The TextureBrush class is a brush made up of an image. It allows you to fill shapes with various versions of an image. |
HatchBrush |
The HatchBrush class is used to create a brush based on a predefined pattern, a foreground color, and a background color. |
LinearGradientBrush |
The LinearGradientBrush is used to create brushes that blend two colors across an object. |
PathGradientBrush |
The PathGradientBrush can create a brush object that can be used to fill paths. |
Graphic Storage |
|
Region |
The Region class is used to describe the inside of a graphics shape made of rectangles and paths. |
RegionData |
The RegionData class is used to store the data that makes up a region. |
GraphicsPath |
The GraphicsPath class groups connected lines and curves for manipulation as a whole. |
PathData |
The PathData class is used to store data that makes up a GraphicsPath. |
Utility Classes |
|
Point |
The Point structure allows you to group x and y coordinates as a single object or point on a 2D plane. |
Size |
Similar to the Point structure, the Size structure groups width and height. |
Matrix |
The Matrix class is the mathematical foundation used to transform graphics. |