Changes

Drawing Graphics in C Sharp

1,647 bytes added, 20:59, 28 January 2008
no edit summary
<pre>
private void Form1_Paint(object sender, PaintEventArgs e)
{
System.Drawing.Graphics graphicsObj;
graphicsObj = this.CreateGraphics();
}
</pre>
 
Now that we have a Graphic Object we need a Pen with which to draw.
 
== Creating a Pen In C# ==
 
A Graphics Object is of little use without a Pen object with which to draw (much as a sheet of paper is no good without a pen or pencil with which to draw). A Pen object may be quite easily created as follows:
 
Pen ''variable_name'' = new Pen (''color'', ''width'');
 
where ''variable_name'' is the name to be assigned to the Pen object, ''color'' is the color of the pen and ''width'' is the width of the lines to be drawn by the pen.
 
For example, we can create red pen that is 5 pixels wide as follows:
 
<pre>
private void Form1_Paint(object sender, PaintEventArgs e)
{
System.Drawing.Graphics graphicsObj;
 
graphicsObj = this.CreateGraphics();
 
Pen myPen = new Pen(System.Drawing.Color.Red, 5);
}
</pre>
 
Once a Pen object has been created other properties may be changed. For example, the DashStyle property can be modified to change the style of line (i.e Dash, DashDot, DashDotDot, Dot, Solid or Custom). Properties such as the color and width may similarly be changed after a Pen has been created:
 
<pre>
myPen.DashStyle = System.Drawing.Drawing2D.DashStyle.DashDotDot;
 
myPen.Color = System.Drawing.Color.RoyalBlue;
 
myPen.Width = 3;
</pre>
 
Now that we have a Paint() event handler, a Graphics Object and Pen we can now begin to draw.
 
== Drawing Lines in C# ==