Texture Brushes in GDI+

This article has been excerpted from book "Graphics Programming with GDI+".

Texture brushes allow us to use an image as a brush and fill GDI+ objects with the brush. Texture brushes are useful when you need to fill a graphics object with images in a pattern such as tile. In this section we will discuss how to create and use texture brushes in GDI+.

Figure 4.5.jpg

FIGURE 4.5: The default hatch style rectangle

Figure 4.6.jpg

FIGURE 4.6: The LightDownwardDiagonal style with different colors

Figure 4.7.jpg

FIGURE 4.7: The DiagonalCross style

In the .NET Framework library, the TextureBrush class represents a texture brush. Table 4.2 describes the properties of the TextureBrush class.

Let's create an application using textures brushes. We create a Windows application. We also add a context menu to the form, along with five context menu items. The final looks like Figure 4.8.

TABLE 4.2: TextureBrush properties




Return the Image object associated with a TextureBrush object.


Represents a Matrix object that defines a local geometric transformation for the image.


Represents a WrapMode enumeration that indicates the wrap mode for a texture brush.


FIGURE 4.8: The texture brush application

Note: The WrapMode enumeration represents the wrap mode for a texture brush. It has five members: Clamp, Tile, TileFlipX, TileFlipY and TileFlipXY. These members are described later, in Table 4.7.

Now we add a class-level variable of TextureBrush type to the application:

private TextureBrush txtrBrush = null;

The next step is to create a texture brush from an image and fill a rectangle with that brush. We create an Image object on the form's load event handler from the file Image.gif, which is used to create a TextureBrush object. On the form's paint event handler, we call the FillRectangle method to fill the rectangle with the texture. Listing 4.8 shows the form's load and paint event handler. Note that our rectangle is the ClientRectangle of the form.

LISTING 4.8: Creating a texture brush and filling a rectangle

void Form1_Load (object sender, System.EventArgs e)
//Create an image from a file
            Image img = new Bitmap("Image.GIF");
//Create a texture brush from an image
            txtrBrush = new TextureBrush (img);

void Form1_Paint (object sender, System.Windows.Forms.PaintEventArgs e)
            Graphics g = e.Graphics;
//Fill a rectangle with a texture brush
            g.FillRectangle (txtrBrush, ClientRectangle);

Now we can add event handlers for the context menu items as shown in Listing 4.9. As you can see from this code, we simply set the WrapMode property of the texture brush.

LISTING 4.9: TextureBrush's context menu event handlers

void Clamp_Click(object sender, System.EventArgs e)
                  txtrBrush.WrapMode = WrapMode.Clamp;

private void Title_Click(object sender, System.EventArgs e)
                  txtrBrush.WrapMode = WrapMode.Tile;

private void TitleFlipX_Click(object sender, System.EventArgs e)
                  txtrBrush.WrapMode = WrapMode.TileFlipX;

private void TileFlipY_Click(object sender, System.EventArgs e)
                  txtrBrush.WrapMode = WrapMode.TileFlipY;

private void TileFlipXY_Click(object sender, System.EventArgs e)
                  txtrBrush.WrapMode = WrapMode.TileFlipXY;

Finally, we need to load the context menu on the right mouse click event handler. As Listing 4.10 shows, we simply set the ContextMenu property of the form.

LISTING 4.10: The right mouse button click event handler

void Form1_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
if (e.Button == MouseButtons.Right)
this.ContextMenu = contextMenu1;

Now let's run the application. Figure 4.9 shows default (tiled) output from the program. The entire client rectangle is filled with texture.

If we right-click on the form and select the Clamp menu item, we get Figure 4.10.

Now let's select the TileFlipY option, which generates Figure 4.11.

You can try other options on your own!

Figure 4.9.jpg

FIGURE 4.9: Using Texture brushes


FIGURE 4.10: Claming a texture

Figure 4.11.jpg

FIGURE 4.11: The TileFlipY texture option


Hope the article would have helped you in understanding Texture Brushes in GDI+. Read other articles on GDI+ on the website.

This book teaches .NET developers how to work with GDI+ as they develop applications that include graphics, or that interact with monitors or printers. It begins by explaining the difference between GDI and GDI+, and covering the basic concepts of graphics programming in Windows.