Changes

Jump to: navigation, search

Drawing Graphics using PowerShell 1.0 and GDI+

2,085 bytes added, 20:01, 18 December 2008
The Paint Event Handler
}
)
</pre>
== Drawing Graphics with Windows PowerShell and GDI+ ==
 
As outlined in the preceding section, the code to draw the graphics is contained in the paint event handler. In this section we will look at some basic drawing methods.
 
The following example draws a line from the point at co-ordinates x=10, y=10 to the point at x=190, y=190. It does so using the previously created $myPen object.
 
<pre>
$formGraphics.DrawLine($pen, 10, 10, 190, 190)
</pre>
 
A filled ellipse is drawn by calling the ''FillEllipse()'' method of the graphics object, passing through a suitable drawing object (e.g a brush or a pen) and defining a rectangle into which the ellipse is to fit:
 
<pre>
$formGraphics.FillEllipse($myBrush, 20, 20, 180, 180)
</pre>
 
[[Image:windows_powershell_gdi_ellipse.jpg|A filled ellipse drawn using Windows PowerShell and GDI+]]
 
Similarly, a filed rectangle can be drawn using the ''FillRectange()'' method, once again passing through a pen or brush and defining the rectangle:
 
<pre>
$formGraphics.FillRectangle($myBrush, 0,0,200,200)
</pre>
 
[[Image:windows_powershell_gdi_filled_rectangle.jpg|A filled rectangle drawn using Windows PowerShell and GDI+]]
 
In the above example, rectangles were defined by passing co-ordinates directly through as arguments to the drawing methods. It is also possible to create Rectangle objects:
 
<pre>
$rect = new-object Drawing.Rectangle 0, 0, 200, 200
 
$formGraphics.FillRectangle($myBrush, $rect)
</pre>
 
No graphics drawing tutorial would be complete without a single bezier spline, and this chapter is no exception. The following example defines the start, end and two control points of a bezier spline and passes them through, along with a pen object, to the ''DrawBezier()'' method of the graphics object:
 
<pre>
$p1 = new-object Drawing.Point 10, 100
$p2 = new-object Drawing.Point 100, 10
$p3 = new-object Drawing.Point 170, 170
$p4 = new-object Drawing.Point 200, 100
</pre>
 
The above script will result in the following:
 
[[Image:windows_powershell_gdi_bezier.jpg|A Bezier spine drawn using Windows PowerShell and GDI+]]
== Drawing Graphics with Windows PowerShell and GDI+ ==

Navigation menu