Export (0) Print
Expand All

How to: Draw 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.

Example

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 DrawImage draws the bitmap with its upper-left corner at (0, 0). The call to DrawEllipse uses the Pen object to draw a textured ellipse.

The following illustration shows the bitmap and the textured ellipse.

Pens
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);

Compiling the Code

Create a Windows Form and handle the form's Paint event. Paste the preceding code into the Paint event handler. Replace Texture.jpg with an image valid on your system.

See Also

Community Additions

ADD
Show:
© 2014 Microsoft