Categories
GDI + Uncategorized

Exploring GDI+ : Using the Pen

In my tour in exploring GDI+ I explored the Brush object, now its time for some Pen stuff.

While the Brush classes are used to fill shapes, the Pen class is used to frame shapes. However, Pens are not only used for simple frames. Here in this post I show some advanced uses for the Pen. [more]

Some interesting members are :

sealed class Pen : MarshalByRefObject, ICloneable,  IDisposable 
{  
// Constructors
public Pen(Brush brush);
public Pen(Brush brush, float width);
public Pen(Color color);
public Pen(Color color, float width);
// Properties
public Color Color { get; set; }
public CustomLineCap CustomEndCap { get; set; }
public CustomLineCap CustomStartCap { get; set; }
public DashCap DashCap { get; set; }
public float DashOffset { get; set; }
public float[] DashPattern { get; set; }
public DashStyle DashStyle { get; set; }
public LineCap EndCap { get; set; }
public LineJoin LineJoin { get; set; }
public LineCap StartCap { get; set; }
public float Width { get; set;
}
//Other members removed here.}

As you can see, the pen can be instantiated with a Color or with a defined Brush that means the pen can be one of the Brush TypesI previously wrote about.

In addition to their brushlike behavior, pens have behavior at starting and ending along their length that brushes don’t have. For example, each end can have a different style, as determined by the LineCap enumeration shown next.

Line Caps

1

You can set different LineCaps for pens in the StartCap and EndCap properties of the Pen, there are predefined caps and custom caps are allowed too.

        private void RenderPens1(Graphics g)
{
using (Pen p = new Pen(Color.Black, 10))
{
p.EndCap = LineCap.ArrowAnchor;
g.DrawLine(p, new Point(10, 20), new Point(300, 20));
//other pens
p.EndCap = LineCap.Custom;
// width and height of 3 and unfilled arrow head
p.CustomEndCap = new AdjustableArrowCap(3, 3, false);
g.DrawLine(p, new Point(10, 260), new Point(300, 260));
}

Dashes

In addition to the ends having special styles the starts can have as well, further more a line can have a dash style, as defined by the DashStyle enumeration.

Dash Style of Pen

        private void RenderPens2(Graphics g)
{
using (Pen p = new Pen(Color.Black, 10))
{ p.DashStyle = DashStyle.Dash;
g.DrawLine(p, new Point(10, 20), new Point(300, 20));
//other DashStyles
p.DashStyle = DashStyle.Custom;
p.DashPattern = new float[] { 1f, .5f, 2f, .5f, 3f, .5f, 4f };
g.DrawLine(p, new Point(10, 170), new Point(300, 170)); } }

As you can notice in the members of the Pen, the DashCap property which also accepts a LineCap enumeration, so you can take more control over the line that appears.
Thats all for this part. Happy Coding.

PensForm.cs (2.86 kb)



		
Categories
GDI + Uncategorized

Exploring GDI+ : Using The Brush

Brush HierarchyOne of the core classes in the GDI+ framework built in .Net is the Brush class. There are 5 types of Brushes in .Net that help you to color your objects by lot of variations the five types are as shown in the figure :

 

[more] 

The 5 types as shown are :

SolidBrush Defines a brush of a single color. Brushes are used to fill graphics shapes, such as rectangles, ellipses, pies, polygons, and paths.
HatchBrush Defines a rectangular brush with a hatch style, a foreground color, and a background color.
LinearGradientBrush Encapsulates a Brush with a linear gradient.
PathGradientBrush Encapsulates a Brush object that fills the interior of a GraphicsPath object with a gradient.
TextureBrush Each property of the TextureBrush class is a Brush object that uses an image to fill the interior of a shape.

I have built a simple application that uses and shows the output of each brush, remember that options of each bursh is not limited to my sample, there are much more one can do using these brushes.

solid hatch linearGradient

pathGradient texture

Sample Application Code :

Exploring GDI+ Brushes.zip (13.24 kb)