Textured Pens and Brushes

You saw in the previous example that a pen can be given a brush with which to paint the line. Brushes can be solid, gradients, textures, or hatch patterns. A textured brush can be used to draw lines that are painted with a bitmap image. To accomplish this, you can load in a bitmap from a file or a resource, create a brush with it, pass the brush to a pen, and finally draw your lines with the pen. The following snippet of code shows this process:

Image i=Image.FromFile("stone.bmp");
TextureBrush b= new TextureBrush(i);
Pen p=new Pen(b,10);
e.Graphics.DrawLine(p,0,0,100,100);
p.Dispose();

As you can see, a ridiculously simple thing to do.

Get C# and the .NET Framework: The C++ Perspective now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.