This site was last updated:Monday 13 October 2008, 11:30 GMT

Educational Community

DRAWING GRAPHICS IN C#
(2 votes, average: 3.50 out of 5)
Written by Christopher   
Sunday, 15 June 2008 18:32

INTRODUCTION

In a previous tutorial we talked about graphics in C# and the .Net environment. In this tutorial we will present the basic steps for drawing graphics.

HOW TO SPECIFY LOCATION AND SIZE OF CONTROLS

One of the most common uses for the System.Drawing namespace is specifying the location and size of controls in a windows form application. To specify a control’s location you must create a point structure that corresponds to the relative point you want to define. The coordinates of this structure are relative to the upper left corner of the form. The point structure accepts integer values, while the pointF structure takes floating values. However, the pointF cannot be used to specify the location of controls. To move a button 10 pixels from the top and left sides of the form use the following code:

 

button1.Location = new point(10, 10);

 

The same procedure is used to change the colors of controls. You change the ForeColor or BackColor property of the appropriate controls.

 

DRAW LINES AND SHAPES

To draw on a form control you should conduct the necessary steps:

 

·         Create a Graphics object by calling the appropriate method.

·         Create a pen object.

·         Call a member of the graphics class to draw on the control using the pen object.

·          

Once you create the graphics object you can call many methods to complete the drawing:

 

·         Clear: Clears the drawing surface and fills it with a specified color.

·         DrawLine: draws a line connecting two points.

·         DrawLines: Draws a series of lines that connect an array of Point structures.

·         DrawPolygon: Draws a shape as defined by an array of Point structures.

·         DrawRectangle: Draws a rectangle specified by a coordinate pair, a width and a length parameter.

 

 

In order to use these methods you have to provide an instance of the Pen class. You can specify the pen’s color and width in pixels before drawing. The pen class provides several dash styles you can choose from. Finally, you can control the end caps in order to draw arrows or other special effects. Copy the following code in a windows form application and see its result. Remember to add a button first:

 

private void button1_Click(object sender, EventArgs e)

        {

            Graphics myG = this.CreateGraphics();

            Pen myP = new Pen(Color.Red);

            myP.EndCap = LineCap.RoundAnchor;

            myP.DashStyle = DashStyle.Dash;

            myP.Width = 8;

            Point p1 = new Point(10,10);

            Point p2 = new Point(200,10);

            myG.DrawLine(myP, p1, p2);

        }

 

Instead of a line we could have created a polygon or an arc. However, these objects would be empty on the inside. In order to fill shapes the Graphics class has Fill methods that draw a shape and fill in the contents. The only difference is that these methods require an instance of a Brush class instead of a Pen class. Actualy, you do not instantiate the Brush class but one of its derived classes such as:

 

·         SolidBrush: Defines a brush of solid color.

·         TextureBrush: Defines a brush made from an image.

·         LinearGradientBush:  brush with linear gradient providing an appealing look.

 

In order to fill a polygon, for example you have to call the suitable method GraphicsObject.FillPolygon(). You can also combine the Draw and Fill methods to create solid objects with outlines.

Trackback(0)
Comments (1)add comment

sepideh said:

hi
i need one sourcecode Draw polygon with C#
 
report abuse
vote down
vote up
August 16, 2008
Votes: +0

Write comment
quote
bold
italicize
underline
strike
url
image
quote
quote
smile
wink
laugh
grin
angry
sad
shocked
cool
tongue
kiss
cry
smaller | bigger

busy
 

User Menu

None
Become a sponsor Become a sponsor Become a sponsor Become a sponsor Become a sponsor KNOGE.com offers free video and text tutorials on various softwares also free resources to improve your economy and start making money online. THIS IS ONLY A TEST AD TO DISPLAY HOW IT MIGHT LOOK Become a sponsor Become a sponsor Become a sponsor Become a sponsor Become a sponsor Become a sponsor Become a sponsor Become a sponsor Become a sponsor Become a sponsor Become a sponsor Become a sponsor Become a sponsor Become a sponsor

Adobe InDesign CS3 - Working with objects

Objects in Adobe InDesign CS3 are the basic building block of any design that you do. In this video tutorial you will learn how to create those blocks easily in...

InDesign Videos | Christopher

Read More

Adobe InDesign CS3 - Working with Panels & More

In this Adobe InDesign CS3 video tutorial you will learn how to set keys and work with panels, you will also learn how to create customized keyboard shortcuts for optimized...

InDesign Videos | Christopher

Read More

Adobe Photoshop CS3 - Create customized rust on cars

This Adobe Photoshop CS3 video tutorial will teach you how to create your own customized rust on cars for a more grungy and perhaps dusty effect. This technique does also...

Photoshop Videos | Christopher

Read More
100%
-
+
3
Show options