Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


advertisement
 

The Basics of GDI+ : Page 2

GDI+ is .NET's way to render low-level graphics. GDI+ uses drawing "primitives," such as lines, curves, circles, and rectangles. In addition, GDI+ supports image manipulation as well as text rendering.


advertisement

Simple Line Drawings
A fundamental task performed by the graphics-object, is drawing lines and curves. You can use a number of methods for this purpose. In GDI+, unlike in regular (older) GDI, drawing lines and filling areas are two entirely different operations. When you draw lines, you must consider a number of fundamental things. For example, you need to choose what kind of line to draw.. Do you want a straight line, or a curve? Do you want a simple, single line, or do you want to draw a complex line composed out of many segments? Maybe you want to draw a closed shape that forms a completely enclosed area with an identical start and end point (such as a circle, rectangle, or polygon). Depending on the desired shape of the line, different methods are available to generate them.

Figure 1: Drawing lines using different Pens.
Less obvious than the position and shape of the line, are the parameters for a line. You might ask, "What parameters can I specify for a line?" You can specify the following parameters: attributes such as color and thickness, start and end-points, and the shape of the end of a line such as whether the line ends in a rounded or square "head" or ends in an arrow.

In GDI+, lines are represented by Pen objects. Pens encapsulate all the attributes described above. GDI+ provides a number of default Pen objects, such as pens of different colors. The following code demonstrated drawing a simple straight line using different pens. (This is VB .NET code. C# developers add a semi-colon at the end of the line):

   g.DrawLine(Pens.Red,10,10,200,100)
   g.DrawLine(Pens.Green,10,30,200, 120)
   g.DrawLine(Pens.Blue,10,50,200,140)
If you want to adjust the thickness of the used pen, you need to instantiate a custom pen object. This example generates a five-pixel thick red pen and uses it to draw another line:

   g.DrawLine( _   New Pen(Color.Red, 5), _   10, 100, 200, 190)
You instantiate the Pen using the line color and thickness as parameters. Once again, the C# version of the code is very similar: Simply add a semi-colon at the end and write the "new" keyword in lower case.

Figure 1 shows the result of all four lines of code listed above.

If you play with the DrawLine() method a bit, you will discover that it has a large number of overloads, though the result of these overloads is the same. You can just take different paths to your destination. I encourage you to experiment with the different options. For instance, you can draw circles and ellipses:



   g.DrawEllipse(Pens.Red, _   10, 10, 150, 80)
Similarly, you can draw rectangles with this code:

   g.DrawRectangle(Pens.Green, _   20, 100, 120, 60)
The following example draws a Bezier curve. Explaining the details of Bezier curves is beyond the scope of this article (Time to drag out your old math text books...):

   g.DrawBezier(Pens.Blue, _  170, 10, 250, 90, 170, 90, 250, 180)
Figure 2 shows the result of these 3 drawing operations.



Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap
Thanks for your registration, follow us on our social networks to keep up-to-date