Share via


Drawing a Line Filled with a Texture

Instead of drawing a line with a solid color, you can draw a line with a texture. To draw lines and curves with a texture, create a TextureBrush object, and pass that TextureBrush object to a Pen constructor. The bitmap associated with the texture brush is used to tile the plane (invisibly), and when the pen draws a line or curve, the stroke of the pen uncovers certain pixels of the tiled texture.

The following example creates a Bitmap object from the file Texture1.jpg. That bitmap is used to construct a TextureBrush object, and the TextureBrush object is used to construct a Pen object. The call to Graphics.DrawImage draws the bitmap with its upper-left corner at (0, 0). The call to Graphics.DrawEllipse uses the Pen object to draw a textured ellipse.

Dim bitmap As New Bitmap("Texture1.jpg")
Dim tBrush As New TextureBrush(bitmap)
Dim texturedPen As New Pen(tBrush, 30)

e.Graphics.DrawImage(bitmap, 0, 0, bitmap.Width, bitmap.Height)
e.Graphics.DrawEllipse(texturedPen, 100, 20, 200, 100)
[C#]
Bitmap bitmap = new Bitmap("Texture1.jpg");
TextureBrush tBrush = new TextureBrush(bitmap);
Pen texturedPen = new Pen(tBrush, 30);

e.Graphics.DrawImage(bitmap, 0, 0, bitmap.Width, bitmap.Height);
e.Graphics.DrawEllipse(texturedPen, 100, 20, 200, 100);     

The following illustration shows the bitmap and the textured ellipse.

swtkt67b.pens7(en-us,VS.71).gif