Using Pens and Brushes in GDI+

To understand the features implemented in this iteration requires more knowledge about GDI+. Users will draw lines in different colors and use transparent brushes, so let's look at color and transparency.

7.5.1 Color in GDI+: Using the RGB and ARGB Formats

Until this point we have used the predefined system colors, such as Pens.Black, to draw the graphics primitives. These predefined colors are internally converted into RGB values via a lookup table. For more advanced image processing, which will be used in this iteration, it is necessary to understand how color components are handled internally. Therefore, this section describes the theory of color components.

As mentioned earlier, image data is stored internally in a two-dimensional raster. Each position in the raster, or each pixel, is stored using four color components. Three components of the pixel storage are used to define the color of the pixel. The fourth component is used to describe the transparency or opacity of a given pixel. The transparency or opacity value is also referred to as the alpha component.

The Alpha Component

Figure 7.3 shows the breakdown of each stored pixel into its components. As mentioned before, the alpha value describes the transparency or opacity of a pixel. The alpha component is specified as an integer in the range of 0 to 255, where 0 means the pixel is completely transparent (the background color is displayed) and 255 means that the pixel is completely opaque. In many cases the alpha value can be omitted, resulting in the GDI+ default alpha value, which makes the pixel look opaque by setting the value to 255. The effect of an alpha value less than 255 can be observed in the SmartNotes prototype application (developed in Chapter 3) or in the frame feature, which we implement in this chapter.

Figure 7.3. Pixel Components

The RGB Components

The combination of the red, green, and blue (RGB) components describes the color for each pixel in the image. Typically, each component is stored as an 8-bit value, and this means that each color component can hold a value between 0 and 255. Thus, the color value can have one of 2^(8*3) = 16,777,216 different values. Even though 8 bits per color component is typical, different sizes can be used. When you work with image data, it is essential to know, or to find out, what data size is used for the color components in the image.

Pixel Size as a Measure of Bits Per Pixel

Often in image processing, the term bits per pixel (bpp) is used to describe the memory size needed to store one pixel of the image. For an image stored in alpha RGB (ARGB) format with each component of 8 bits in size, the bits per pixel value would be 4*8 = 32 bpp. Omitting the alpha value would result in 24 bpp RGB image data. Knowing the bits per pixel, together with the image raster size (width and height), is essential for working with image data. It enables us to calculate the memory size that needs to be allocated for an image. We calculate the memory size by multiplying the width by the height and the bpp of the image. An example of this is shown in Chapter 8.

7.5.2 More on Pens and Brushes

Chapter 6 explains how to use a pen to draw the graphics objects. But pens and brushes offer a much wider variety of features than just drawing a simple line or filling a shape with a color. In the course of this book we use more advanced features of pens and brushes. For a complete reference on features provided for pens and brushes, please consult the MSDN documentation. We recommend that you explore additional features of GDI+ by adding new features to the application based on the sample solution provided with this chapter.

Brushes

GDI+ provides five brush types, each of which is defined by a class. Table 7.2 gives you a quick overview of these five classes. All the brush classes are declared sealed, and therefore they cannot be inherited by any other class.

Table 7.2. The Five Brush Classes in GDI+

Brush

Description

SolidBrush

Defines a brush of a single color. Brushes are used to fill graphics shapes, such as rectangles, ellipses, pies, polygons, and paths.

HatchBrush

Defines a rectangular brush with a hatch style, a foreground color, and a background color. There are six hatch styles provided.

This class encapsulates both two-color gradients and custom multicolor gradients.

LinearGradientBrush

All linear gradients are defined along a line specified either by the width of a rectangle or by two points.

By default, a two-color linear gradient is an even horizontal linear blend from the starting color to the ending color along the specified line.

Encapsulates a Brush object that fills the interior of a GraphicsPath object with a gradient.

PathGradientBrush

The color gradient is a smooth shading of colors from the center point of the path to the outside boundary edge of the path. Blend factors, positions, and style affect where the gradient starts and ends, and how fast it changes shade.

TextureBrush

Each property of the TextureBrush class is a Brush object that uses an image to fill the interior of a shape.

Brushes provide the developer with a wide variety of tools to fill shapes using different effects. It is outside the scope of this book to cover all the possible functionalities. Instead, we provide the basics you need to start working with brushes and pens and give enough details to enable you to explore more of the provided features.

Pens

To draw the geometric shapes introduced in Chapter 6, we use a pen with a solid color. GDI+ provides a single class for pens to be used for drawing. However, pens are slightly more complex than brushes because pens allow more properties to be specified to customize them. Table 7.3 lists some of the public properties of the Pen class.

Table 7.3 shows that using a pen can be quite powerful. We'll show you how to use a pen and how to set various properties on a limited number of examples. We recommend that you extend the application further in order to learn about the capabilities of the Pen class.

Table 7.3. The Public Pen Properties

Pen Property

Description

Alignment

Gets or sets the alignment of the pen in relation to the border of the object drawn.

Brush

Gets or sets a brush object that can be used to fill a drawn line.

Color

Gets or sets the color of this pen object.

CompoundArray

Gets or sets an array of values that specify a compound pen. A compound pen draws a compound line made up of parallel lines and spaces.

XXXXCap

Gets or sets different options of how the line start or end is drawn (e.g., whether the ends are drawn round, as arrowheads, and so on).

DashYYYY

Gets or sets different properties of a line that is drawn with a dash pattern.

LineJoin

Gets or sets the join style for the ends of two consecutive lines drawn with this Pen object.

Transform

Gets or sets the geometric transformation for this Pen object.

Width

Gets or sets this pen width.

7.5.3 The GraphicsPath Class

The GDI+-provided GraphicsPath class is used by applications to draw a series of lines and curves in order to describe an outline of shapes. In addition, GraphicsPath enables developers to access other functionality related to the outlined shape:

As you can see, GraphicsPath provides the application developer with lots of functionalities related to drawing and working with concatenated lines. In the case of the photo editor, GraphicsPath could be used to draw user-defined open or closed shapes. For more information on GraphicsPath, search the help documentation provided with Visual Studio.NET.

7.5.4 The Region Class

A Region object describes a region that is zero or more areas of real estate on the drawing surface. In addition, by using two or more GraphicsPath objects, you can create a region as the result of mathematical operations such as algebraic set operations (see Figure 7.4).

Figure 7.4. Region Object Algebraic Set Methods

Furthermore, the Region class provides many additional methods to manipulate and query regions. It is outside the scope of this book to explain all the functionality provided. For a complete list of functionalities, please consult the MSDN help documentation of the Region class.

Категории