Deitel & Associates, Inc. Logo

Back to www.deitel.com
digg.png delicious.png blinkit.png furl.png
Visual C# 2005
How to Program, 2/e

ISBN:
0-13-152523-9
© 2006
pages: ~1500

Order
Amazon logo

This tutorial demonstrates the powerful graphics capabilities of C#'s Graphics class, including shapes, brushes, pens, textures, gradients, line styles and general paths (which can be used to create your own two-dimensional shapes). The tutorial is intended for students and professionals who are already familiar with C# graphical user interface fundamentals. Download the examples for this tutorial here.

[Notes: This tutorial is an excerpt (Section 17.9) of Chapter 17, Graphics and Multimedia, from our textbook Visual C# 2005 How to Program, 2/e. This tutorial may refer to other chapters or sections of the book that are not included here. Permission Information: Deitel, Harvey M. and Paul J., Visual C# How to Program, 2/E ©2006. Electronically reproduced by permission of Pearson Education, Inc., Upper Saddle River, New Jersey.]

Two-Dimensional Graphics
C# offers many additional graphics capabilities. The Brush hierarchy, for example, also includes HatchBrush, LinearGradientBrush, PathGradientBrush and TextureBrush.
Gradients, Line Styles and Fill Patterns
The program in Fig. 17.21 demonstrates several graphics features, such as dashed lines, thick lines and the ability to fill shapes with various patterns. These represent just a few of the additional capabilities of the System.Drawing namespace.
Lines 18-88 define the DrawShapesForm Paint event handler. Lines 25-27 create an object of class LinearGradientBrush named linearBrush. A LinearGradientBrush (namespace System.Drawing.Drawing2D) enables users to draw with a color gradient.
Fig. 17.21 Shapes drawn on a form. (Part 1 of 2.)
1   // Fig. 17.21: DrawShapes.cs
2   // Drawing various shapes on a Form.
3   using System;
4   using System.Drawing;
5   using System.Drawing.Drawing2D;
6   using System.Windows.Forms;
7   
8    // draws shapes with different brushes
9    public partial class DrawShapesForm : Form
10    {
11       // default constructor
12       public DrawShapesForm()
13       {
14          InitializeComponent();
15       } // end constructor
16   
17       // draw various shapes on Form
18       private void DrawShapesForm_Paint( object sender, PaintEventArgs e )
19       {
20          // references to object we will use
21         Graphics graphicsObject = e.Graphics;
22   
23          // ellipse rectangle and gradient brush
24          Rectangle drawArea1 = new Rectangle( 5, 35, 30, 100 );
25          LinearGradientBrush linearBrush =
26             new LinearGradientBrush ( drawArea1, Color.Blue,
27                Color.Yellow, LinearGradientMode.ForwardDiagonal );
28   
29          // draw ellipse filled with a blue-yellow gradient
30          graphicsObject.FillEllipse( linearBrush, 5, 30, 65, 100 );
31   
32          // pen and location for red outline rectangle
33          Pen thickRedPen = new Pen( Color.Red, 10 );
34          Rectangle drawArea2 = new Rectangle( 80, 30, 65, 100 );
35   
36          // draw thick rectangle outline in red
37          graphicsObject.DrawRectangle( thickRedPen, drawArea2 );
38   
39          // bitmap texture
40          Bitmap textureBitmap = new Bitmap( 10, 10 );
41   
42          // get bitmap graphics
43          Graphics graphicsObject2 =
44             Graphics.FromImage( textureBitmap );
45   
46          // brush and pen used throughout program
47          SolidBrush solidColorBrush =
48             new SolidBrush( Color.Red );
49          Pen coloredPen = new Pen( solidColorBrush );
50   
51          // fill textureBitmap with yellow
52          solidColorBrush.Color = Color.Yellow;
53          graphicsObject2.FillRectangle( solidColorBrush, 0, 0, 10, 10 );
54   
55          // draw small black rectangle in textureBitmap
56          coloredPen.Color = Color.Black;
57          graphicsObject2.DrawRectangle( coloredPen, 1, 1, 6, 6 );
58   
59          // draw small blue rectangle in textureBitmap
60          solidColorBrush.Color = Color.Blue;
61          graphicsObject2.FillRectangle( solidColorBrush, 1, 1, 3, 3 );
62   
63          // draw small red square in textureBitmap
64          solidColorBrush.Color = Color.Red;
65          graphicsObject2.FillRectangle( solidColorBrush, 4, 4, 3, 3 );
66   
67          // create textured brush and
68          // display textured rectangle
69          TextureBrush texturedBrush =
70             new TextureBrush( textureBitmap );
71          graphicsObject.FillRectangle( texturedBrush, 155, 30, 75, 100 );
72   
73          // draw pie-shaped arc in white
74          coloredPen.Color = Color.White;
75          coloredPen.Width = 6;
76          graphicsObject.DrawPie( coloredPen, 240, 30, 75, 100, 0, 270 );
77   
78          // draw lines in green and yellow
79          coloredPen.Color = Color.Green;
80          coloredPen.Width = 5;
81          graphicsObject.DrawLine( coloredPen, 395, 30, 320, 150 );
82   
83          // draw a rounded, dashed yellow line
84          coloredPen.Color = Color.Yellow;
85          coloredPen.DashCap = DashCap.Round;
86          coloredPen.DashStyle = DashStyle.Dash;
87          graphicsObject.DrawLine( coloredPen, 320, 30, 395,v150 );
88       } // end method DrawShapesForm_Paint
89    } // end class DrawShapesForm
DrawingShapes
Page 1 | 2 | 3