Chapter 19 - Graphics with GDI+ | |
bySimon Robinsonet al. | |
Wrox Press 2002 | |
We've almost finished the first part of the chapter, in which we've covered all the basic classes and objects required in order to draw specified shapes and so on to the screen. We'll round off by reviewing some of the drawing methods the Graphics class makes available, and presenting a short example that illustrates the use of several brushes and pens.
System.Drawing.Graphics has a large number of methods that allow you to draw various lines, outline shapes, and solid shapes. Once again there are too many to provide a comprehensive list here, but the following table gives the main ones and should give you some idea of the variety of shapes you can draw.
Method | Typical parameters | What it draws |
---|---|---|
DrawLine | Pen, start and end points | A single straight line |
DrawRectangle | Pen, position, and size | Outline of a rectangle |
DrawEllipse | Pen, position, and size | Outline of an ellipse |
FillRectangle | Brush, position, and size | Solid rectangle |
FillEllipse | Brush, position, and size | Solid ellipse |
DrawLines | Pen, array of points | Series of lines, connecting each point to the next one in the array |
DrawBezier | Pen, 4 points | A smooth curve through the two end points, with the remaining two points used to control the shape of the curve |
DrawCurve | Pen, array of points | A smooth curve through the points |
DrawArc | Pen, rectangle, two angles | Portion of circle within the rectangle defined by the angles |
DrawClosedCurve | Pen, array of points | Like DrawCurve but also draws a straight line to close the curve |
DrawPie | Pen, rectangle, two angles | Wedge -shaped outline within the rectangle |
FillPie | Brush, rectangle, two angles | Solid wedge-shaped area within the rectangle |
DrawPolygon | Pen, array of points | Like DrawLines but also connects first and last points to close the figure drawn |
Before we leave the subject of drawing simple objects, we'll round off with a simple example that demonstrates the kinds of visual effect you can achieve by use of brushes. The example is called ScrollMoreShapes , and it's essentially a revision of ScrollShapes . Besides the rectangle and ellipse, we'll add a thick line and fill the shapes in with various custom brushes. We've already explained the principles of drawing so we'll present the code without too many comments. First, because of our new brushes, we need to indicate we are using the System.Drawing.Drawing2D namespace:
using System; using System.Drawing; using System.Drawing.Drawing2D; using System.Collections; using System.ComponentModel; using System.Windows.Forms; using System.Data;
Next some extra fields in our Form1 class, which contain details of the locations where the shapes are to be drawn, as well as various pens and brushes we will use:
private Rectangle rectangleBounds = new Rectangle(new Point(0,0), new Size(200,200)); private Rectangle ellipseBounds = new Rectangle(new Point(50,200), new Size(200,150)); private Pen bluePen = new Pen(Color.Blue, 3); private Pen redPen = new Pen(Color.Red, 2); private Brush solidAzureBrush = Brushes.Azure; private Brush solidYellowBrush = new SolidBrush(Color.Yellow); static private Brush brickBrush = new HatchBrush(HatchStyle.DiagonalBrick, Color.DarkGoldenrod, Color.Cyan); private Pen brickWidePen = new Pen(brickBrush, 10);
The brickBrush field has been declared as static, so that we can use its value to initialize the brickWidePen field. C# won't let us use one instance field to initialize another instance field, because it's not defined which one will be initialized first, but declaring the field as static solves the problem. Since only one instance of the Form1 class will be instantiated , it is immaterial whether the fields are static or instance fields.
Here is the OnPaint() override:
protected override void OnPaint( PaintEventArgs e ) { base.OnPaint(e); Graphics dc = e.Graphics; Point scrollOffset = this.AutoScrollPosition; dc.TranslateTransform(scrollOffset.X, scrollOffset.Y); if (e.ClipRectangle.Top+scrollOffset.X < 350 e.ClipRectangle.Left+scrollOffset.Y < 250) { dc.DrawRectangle(bluePen, rectangleBounds); dc.FillRectangle(solidYellowBrush, rectangleBounds); dc.DrawEllipse(redPen, ellipseBounds); dc.FillEllipse(solidAzureBrush, ellipseBounds); dc.DrawLine(brickWidePen, rectangleBounds.Location, ellipseBounds.Location+ellipseBounds.Size); } }
As before we also set the AutoScrollMinSize to (250,350). Now the results:
Notice that the thick diagonal line has been drawn on top of the rectangle and ellipse, because it was the last item to be painted .