Article

# Draw a US Flag using C# and GDI+

By , 7 Dec 2007
 Rate this:

## Introduction

Visual C# provides a powerful GDI+ class library interface that allows users to draw various graphics objects.

This article shows you how to create a US flag using C# and GDI+. The US flag contains 50 star polygons and several rectangles.

## Background

Polygon is one of the most important graphics objects we are dealing with when rendering 2D and 3D graphics or processing computational geometry. `Graphics.DrawPolygon `method draws a polygon defined by an array of point structures. Every pair of two consecutive points in the array specifies a side of the polygon.

Here, I will show you how to create a US flag object. First we need to define the coordinates of a star. As illustrated in the following figure, suppose that the center coordinates of the star are at (xc, yc), r1 is the radius of the inner circle, and r is the radius of the outer circle. The angles a = 72 degrees and ß = 36 degrees.

From this figure, we can easily determine the coordinates of points 0 to 9, as listed in the following table:

 Points x coordinates y coordinates 0 xc yc – r 1 xc + r1 sinß yc – r1 cosß 2 xc + r sina yc – r cosa 3 xc + r1 sina yc + r1 cosa 4 xc + r sinß yc + r cosß 5 xc yc + r1 6 xc – r sinß yc + r cosß 7 xc – r1 sina yc + r1 cosa 8 xc – r sina yc – r cosa 9 xc – r1 sinß yc – r1 cosß

We first implement a `DrawStar `method to draw a single star polygon at the center position (xc, yc) with a size control parameter r (the radius of the outer circle, as shown in the above figure). We then add a `DrawFlag `method that first draws seven red strips on a white rectangle background. Note that the respect ratio of the flag is maintained by setting:

`float height = 10 * width / 19;`

The method then draws the blue rectangle with proper size. Finally we put fifty stars on the blue rectangle uniformly by calling the `DrawStar `method to finish the project.

## Using the Code

The US flag is really drawn by overriding the `OnPaint `method of the `Form1 `class:

```protected override void OnPaint(PaintEventArgs e)
{
Graphics g = e.Graphics;
g.SmoothingMode = SmoothingMode.AntiAlias;
DrawFlag(g, 20, 20, this.Width - 50);
g.Dispose();
}```

Building and running this project produces the following screenshot:

This is just for fun, perhaps even useful. This project is from the examples of the new book "Practical C# Charts and Graphics", where you can find more advanced chart and graphics programming for real-world .NET applications. For more information, please visit my website.

Dr. Jack Xu has a Ph.D in theoretical physics. He has over 15 years programming experience in Basic, Fortran, C, C++, Matlab, and C#, specializing in numerical computation methods, algorithms, physical modeling, computer-aided design (CAD) development, graphics user interface, and 3D graphics. Currently, he is responsible for developing commercial CAD tools based on Microsoft .NET Framework.

United States
No Biography provided

 First PrevNext
 is this... Giuseppe Tollini 14-Aug-13 0:41
 My vote of 5 manoj kumar choubey 18-Feb-12 3:20
 Drawing Sine Waves vinaykskvs 4-Jun-08 2:51
 Easier way to compute the star points Wyld_One 14-Nov-07 21:32
 This is a alternate method to do the stars. It has two important differences.   Only the points are computed in the same order you draw them on a paper with only 5 strokes. (0,4,8,2,6)   The second difference is the FillMode. Using the Winding fill method it becomes aware of the line crossings and what is 'inside' all of the borders. the default FillMode is Alternate.   This way you don't even have to compute the 'inner' circle. ``` private void DrawStar( Graphics g, float r, float xc, float yc ) { // simpler version only five points and only one angle needs computing int xp = 0; // r: determines the size of the star. // xc, yc: determine the location of the star. float radian72 = (float) ( (float) Math.PI * (float) 4.0 ) / (float) 5.0; // Fill the star: PointF[] pts = new PointF[ 5 ]; //Start a loop through all the points for ( xp = 0; xp < 5; xp++ ) { pts[ xp ] = new PointF( xc + r * (float) Math.Sin( xp * radian72 ), (float) yc - r * (float) Math.Cos( xp * radian72 ) ); } g.FillPolygon( Brushes.White, pts, FillMode.Winding); } ```
 Help in graphics programming needed !! Lalito80 19-Jun-07 4:51
 NICE michael_rost 11-Jun-07 9:07
 Your book looks great - I need to order it ASAP Eric Engler 2-Apr-07 10:26
 Re: Your book looks great - I need to order it ASAP Jack JH Xu 2-Apr-07 11:53
 Re: Your book looks great - I need to order it ASAP Eric Engler 2-Apr-07 12:16
 Last Visit: 31-Dec-99 18:00     Last Update: 6-Mar-14 23:44 Refresh 1234 Next »