Changes

Jump to: navigation, search

Drawing Graphics in C Sharp

3,199 bytes added, 17:29, 29 January 2008
no edit summary
== 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: ''graphicsobj''.DrawEllipse(''pen'', ''x'', ''y'', ''width'', ''height''); The alternative is to pass through a Rectangle object in place of the co-ordinates and dimensions. The syntax for creating a Rectangle object in C# is as follows: Rectangle ''rectangleObj'' = new Rectangle (''x'', ''y'', ''width'', ''height''); Once a Rectangle object has been instantiated the syntax to call ''DrawRectangle()'' is as follows: ''graphicsobj''.DrawEllipse(''pen'', ''x'', ''y'', ''rectangleobj''); The following example creates a Rectangle which is then used as an argument to ''DrawEllipse()'': <pre> private void Form1_Paint(object sender, PaintEventArgs e) { System.Drawing.Graphics graphicsObj;  graphicsObj = this.CreateGraphics();  Pen myPen = new Pen(System.Drawing.Color.Green, 5); Rectangle myRectangle = new Rectangle(20, 20, 250, 200); graphicsObj.DrawEllipse(myPen, myRectangle); }</pre> When compiled and executed the above code creates the following graphics output on the form: [[Image:c_sharp_ellipse_drawing.jpg|Drawing an Ellipse with C#]] == Drawing Text with C# == Text is drawn onto a Graphics Object using the ''DrawText()'' method. The syntax for this method is as follows: ''graphicsobj''.DrawString(''string'', ''font'', ''brush'', ''x'', ''y''); The ''string'' argument specifies the text to be drawn. Font defines the font to be used to display the text and requires the creation of a ''Font'' object. The ''brush'' object is similar to the ''Pen'' object used to draw shapes with the exception that it specifies a fill pattern. Finally, the x and y values specify the top left hand corner of the text. In order to create a Font object a font size, font family and font style may be specified. For example to create a Helvietica, 40 point Italic font: <pre> Font myFont = new System.Drawing.Font("Helvetica", 40, FontStyle.Italic); </pre> A brush object is created by specifying by calling the appropriate constructor for the brush type and specifying a color: <pre> Brush myBrush = new SolidBrush(System.Drawing.Color.Red);</pre> Having created the necessary objects we can incorporate these into our example C# application to draw some text: <pre> private void Form1_Paint(object sender, PaintEventArgs e) { System.Drawing.Graphics graphicsObj;  graphicsObj = this.CreateGraphics();  Font myFont = new System.Drawing.Font("Helvetica", 40, FontStyle.Italic);  Brush myBrush = new SolidBrush(System.Drawing.Color.Red);  graphicsObj.DrawString("Hello C#", myFont, myBrush, 30, 30); }</pre> The above code, when compiled and run, will output the following text onto the form: [[Image:c_sharp_drawing_text.jpg|Drawing Text with C#]]

Navigation menu