15.02.2015 Views

C# 4 and .NET 4

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

OC22 ❘ ChaPTer 48 GrAphics with Gdi+<br />

color or a brush, <strong>and</strong> additionally a float, which represents the width of the pen. (It needs to be a float in<br />

case you are using non-default units such as millimeters or inches for the Graphics object that will do the<br />

drawing, so you can, for example, specify fractions of an inch.) For example, you can construct pens similar<br />

to this:<br />

Brush brickBrush = new HatchBrush(HatchStyle.DiagonalBrick,<br />

Color.DarkGoldenrod, Color.Cyan);<br />

Pen solidBluePen = new Pen(Color.FromArgb(0,0,255));<br />

Pen solidWideBluePen = new Pen(Color.Blue, 4);<br />

Pen brickPen = new Pen(brickBrush);<br />

Pen brickWidePen = new Pen(brickBrush, 10);<br />

Additionally, a fourth way offers the quick construction of pens by using the class System.Drawing.Pens,<br />

which, like the Brushes class, contains a number of stock pens. These pens all have a 1-pixel width <strong>and</strong><br />

come in the usual sets of web-safe colors. This allows you to construct pens in this way:<br />

Pen solidYellowPen = Pens.Yellow;<br />

draWing shaPes <strong>and</strong> lines<br />

You have almost finished the first part of the chapter, <strong>and</strong> you have seen all the basic classes <strong>and</strong> objects<br />

required to draw specified shapes <strong>and</strong> so on to the screen. This section starts by reviewing some of the<br />

drawing methods the Graphics class makes available <strong>and</strong> presents a short example that illustrates the use<br />

of several brushes <strong>and</strong> pens.<br />

System.Drawing.Graphics has a large number of methods that allow you to draw various lines, outline<br />

shapes, <strong>and</strong> solid shapes. Again, there are too many to provide a comprehensive list here, but the following<br />

table lists the main ones <strong>and</strong> should give you some idea of the variety of shapes you can draw.<br />

meThod TyPiCal ParameTers WhaT iT draWs<br />

DrawLine Pen, start <strong>and</strong> end points A single straight line.<br />

DrawRectangle Pen, position, <strong>and</strong> size Outline of a rectangle.<br />

DrawEllipse Pen, position, <strong>and</strong> size Outline of an ellipse.<br />

FillRectangle Brush, position, <strong>and</strong> size Solid rectangle.<br />

FillEllipse Brush, position, <strong>and</strong> size Solid ellipse.<br />

DrawLines Pen, array of points Series of lines, connecting each point to the next<br />

one in the array.<br />

DrawBezier Pen, four points A smooth curve through the two endpoints, with<br />

the remaining two points used to control the shape<br />

of the curve.<br />

DrawCurve Pen, array of points A smooth curve through the points.<br />

DrawArc Pen, rectangle, two angles Portion of circle within the rectangle defined by<br />

the angles.<br />

DrawClosedCurve Pen, array of points Like DrawCurve but also draws a straight line to<br />

close the curve.<br />

DrawPie Pen, rectangle, two angles Wedge-shaped outline within the rectangle.<br />

FillPie Brush, rectangle, two angles Solid wedge-shaped area within the rectangle.<br />

DrawPolygon Pen, array of points Like DrawLines but also connects first <strong>and</strong> last<br />

points to close the figure drawn.<br />

www.it-ebooks.info

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!