共用方式為


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

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

Other Resources

Using a Pen to Draw Lines and Shapes
Graphics and Drawing in Windows Forms