Changes

Jump to: navigation, search

Drawing Graphics in C Sharp

29 bytes removed, 20:16, 1 April 2009
no edit summary
The purpose of this chapter of [[C Sharp Essentials|C# Essentials]] is to provide the reader with a knowledge of the basics of graphics drawing in C#. Drawing in C# is achieved using the ''Graphics Object''. The Graphics Object takes much of the pain out of graphics drawing by abstracting away all the problems of dealing with different display devices and screens resolutions. The C# programmer merely needs to create a Graphic Object and tell it what and where to draw.
== Persistent Graphics ==
An important point to note before proceeding with is chapter is that simply creating a Graphics Object for a component and then drawing on that component does not create persistent graphics. In fact what will happen is that as soon as the window is minimized, or obscured by another window the graphics will be erased.
For this reason, steps need to be taken to ensure that any graphics are persistent. Two mechanisms are available for achieving this. One is to repeatedly perform the drawing in the ''Paint()'' event handler of the control (which is triggered whenever the component needs to be redrawn), or to perform the drawing on a bitmap image in memory and then transfer that image to the component whenever the ''Paint()'' event is triggered. We will look at redrawing the graphics in the ''Paint()'' event in this chapter and [[Using Bitmaps for Persistent Graphics in C Sharp|Using Bitmaps for Persistent Graphics in C#]] in the next chapter.
== 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'');
== Drawing Lines in C# ==
Lines are drawn in C# using the ''DrawLine()'' method of the Graphics Object. This method takes a pre-instantiated Pen object and two sets of x and y co-ordinates (the begin start and end points of the line) as arguments. For example, to draw a line from co-ordinates (20, 20) to (200, 210) on our sample form:
<pre>
[[Image:c_sharp_draw_rectangle.jpg|C Sharp Rectangle Drawing Example]]
If multiple rectangles of different shapes need to be drawn it it is not necessary to create a new Rectangle object for each call to the ''DrawRectangle();'' method. Instead the shape of an existing Rectangle object may be altered by calling the ''Inflate()'' method of the Rectangle class. This method accepts two arguments, the amount by which the width is to be changed and the amount by which the height is to be changed. If a dimension is to be left unchanged 0 should be passed through as the change value.
To reduce a dimension pass through the negative amount by which the dimension is to be changed:
== Drawing Ellipses and Circles in C# ==
Ellipses and circles are drawn in C# using the ''DrawEllipse()'' method of the GraphicsObject class. The size of the shape to be drawn is defined by specifying a rectangle into which the shape must fit. As with the ''DrawRectangle()'' method, there are two two ways to use the ''DrawEllipse()'' method. One is to pass through a ''Rectangle'' object and Pen and the other is to create an instance of a Rectangle object and pass that through along with the Pen.
To draw an ellipse without first creating a Rectangle object use the following syntax:

Navigation menu