oreilly.comSafari Books Online.Conferences.


AddThis Social Bookmark Button

System.Drawing with C#

by Budi Kurniawan

The System.Drawing namespace, which contains types that help you with drawing, plays an important role in Windows programming. You need its members to draw a custom control user interface and for sending text and graphics to a printer. Even when you are only using standard controls on your form, you have used some of its members, probably without realizing it. Understanding the System.Drawing namespace enables you to write better -- and probably faster -- code. Here is a 10-minute crash course to familiarize you with it.

There are probably only 10 members of the System.Drawing namespace that you use in 99% of your programs. Out of these, the Point, Color, and Size structures are always present in any form-based applications. Let's start with the easiest ones: Point and PointF, structures that represent a location in a two-dimensional plane; and Size and SizeF, two structures that represent a width and a height. We will continue with the Color structure, as well as the Pen and Brush classes. Finally, the Graphics, Font, Image, and Bitmap classes are discussed. Note that all examples in this article assume that System.Drawing is imported; i.e., the following line is added to the beginning of your class or module:

using System.Drawing;

In the next article, I will demonstrate how these classes and structures are used in Windows printing.

The Point and PointF Structures

A Point object represents a coordinate in a two-dimensional plane. The easiest way to construct a Point is to pass two integers as the abscissa and the ordinate parts of the coordinate, such as the following:

Point myPoint = new Point(1,2);

Point is frequently used not only when drawing a shape, but also when writing a form- based application. For example, to adjust the position of a Button control on a form, you can assign a Point object to the button's Location property to indicate the position of the top-left corner of the button on the form. The following code places the button's top-left corner at coordinate (100, 30) in the form -- i.e., 100 pixels from the left edge of the form, and 30 pixels from the upper edge of the form's client area:

button.Location = new Point(100, 30);

Assigning a Point object to a control's Location property is the shortest way of positioning that control in its container. Another way is to use the Left and Top properties of the control, but this approach requires two lines of code instead of one. The Point structure has the X and Y properties, from which you can obtain the abscissa and ordinate of the coordinate a Point object represents. Also, IsEmpty is a read-only property that returns true only when both the X and Y properties of a Point object have the value of zero. (Note, though, that this can be misleading; (0, 0) can represent a valid coordinate for a Point object.)

You can also construct a Point object by passing a Size object. In this case, the Size object's width will become the abscissa of the Point, and the Size object's height will become the ordinate of the Point object. For example:

Size mySize = new Size(13, 133);
Point myPoint = new Point(mySize);
System.Console.WriteLine("X: " + myPoint.X + ", Y: " + myPoint.Y);

produces the following output:

X: 13, Y: 1

For more information on the Size structure, see the "Size and SizeF Structures" section below. The third way of constructing a Point is by passing an Integer. The low-order 16 bits of the integer will become the value of the Point's abscissa, and the high-order 16 bits the value of the Point's ordinate.

If you need more precision in your Point object, you can use the PointF structure. Instead of representing a coordinate with a pair of integers, PointF takes floats as the X and Y parts of a coordinate. PointF has only a single constructor with the following signature :

public PointF (
  float x, float y

The Size and SizeF Structures

The Size structure represents the width and height of a rectangular area. The easiest way to construct a Size object is by passing two integers as its width and height, as in the following code:

Size mySize = new Size(10, 30);

The Size object above has a width of 10 pixels and a height of 30 pixels. In a form-based Windows application, the Size structure is often used to set or change the size of a control. The Control class has a Size property to which you can assign a Size object. For instance, the following code sets the size of a button so that it will be 90 pixels wide and 20 pixels tall:

button1.Size = new Size(90, 20);

In addition, you can also construct a Size object by passing a Point object, as in the following code:

Point myPoint = new Point(10, 50);
Size mySize = new Size(myPoint);

In this case, the width and height of the resulting Size object will have the value of X and Y properties of the Point object, respectively. You can obtain the width and height values of a Size object by retrieving the value of its Width and Height properties. Another property, IsEmpty, is a read-only property that returns true only if both the Width and Height properties have the value of zero. The SizeF structure is very similar to Size, except that its width and height are represented by float. You use SizeF if you want more precision in your Size object.

The Color Structure

Related Reading

Programming C#
By Jesse Liberty

The Color Structure represents a color that you can use in drawing shapes or to assign to the BackColor or ForeColor properties of a control. To obtain a Color object, you don't use any constructor, because this structure does not have one. Instead, the structure includes a large number of static properties that represent various colors. For example, the Brown property represents a brown Color object. Because these properties are static, you don't need to instantiate a Color object to use them. The following code shows how to use the Brown property of the Color structure to assign a brown color to the BackColor property of a Button control:

Color myColor = Color.Brown;
button1.BackColor = myColor;

In addition to common colors such as green, blue, yellow, red, white, and black, you can choose more exotic colors such as azure, beige, coral, or powder blue. There are more than 140 properties representing different colors.

If that is not enough, you can compose a custom color by passing the R, G, and B components of an RGB color to the FromArgb static method. Again, since this method is static, you can use it without instantiating a Color object. As an example, the following code constructs a Color object whose R, G, and B components are all 240:

Color myColor = Color.FromArgb(240, 240, 240);

Note that even though the arguments are all integers, the values passed must be in the range of 0 and 255; this will result in a 32-bit color, in which the first eight bits represent the alpha value. In fact, you can also specify all 32 bits of a Color object through another overload of the FromArgb method that accepts four integers:

public static Color From Argb(
  int alpha, int red, int green, int blue

You can retrieve the individual alpha, red, green, and blue components of a Color object by calling its toArgb method, which returns an integer representing all of the four components (alpha, red, green, blue). This integer indicates the Color object's color value as follows:

Bits Value
0-7 Blue
8-15 Green
16-23 Red
24-31 Alpha

Another way of constructing a Color object is by using the Color structure's FromName method. For example, the following code constructs a blue color by passing the string blue to the FromName method:

Color myColor = Color.FromName("blue");

Pages: 1, 2, 3

Next Pagearrow