Click here to Skip to main content
Click here to Skip to main content

Draw a US Flag using C# and GDI+

By , 7 Dec 2007
 

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.

About the Author

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.

Please read my other articles:

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Jack J. H. Xu
United States United States
Member
No Biography provided

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
GeneralYour book looks great - I need to order it ASAPmemberEric Engler2 Apr '07 - 10:26 
The table of contents looks very interesting:
 
http://publishing.unicadinc.com/TableContents.htm[^]
 
The price is a bit high, but this kind of content is hard to find. It is worth the price.
GeneralRe: Your book looks great - I need to order it ASAP PinmemberJack JH Xu2 Apr '07 - 11:53 
Hi, Eric,
 
Thank you for your interest in my work.
 
I notice that the price seems little bit high. However you should know that the book is not simply a tutorial. Most of the book content is from my original work. It took me a lot of effort and time to write such a book. In this book, I emphasize the practical usefulness for .NET developers who want to create real-world C# applications. Some examples in the book are actually powerful 2D and 3D chart and graphics packages, which can be directly used in your C# applications. You will find that these example chart and graphics packages alone will be worth much more than the cost of the book.
 
Best.
 
Jack
 
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#.

GeneralRe: Your book looks great - I need to order it ASAP PinmemberEric Engler2 Apr '07 - 12:16 
Indeed, your skill set is hard to find in the .NET world. And these graphics can also be used in ASP.NET by creating bitmapped images, and then streaming them directly to the browser. That way the image never needs to get saved in a hard disk file.
 
There's a lot of good graphics code on Code Project, but it takes a long time to go through it all and make it useful. And most of the graphics code on Code Project comes with very little explanation of how it works. Judging from what I've seen so far, it looks like you do explain your code.
 
As I said, I'll order my book tonight, and I hope you write a sequel someday!
GeneralRe: Your book looks great - I need to order it ASAP PinmemberPaul Selormey22 Apr '07 - 18:03 
So what is up with your publisher? it cannot simply meet the demand. I ordered a copy more than 2 weeks ago on Amazon.com, but it is still no shipped and Amazon.com claims the problem is with their supplier!
 
It will have being better if you simply did, a PDF e-book.
 
Best regards,
Paul.
 
Jesus Christ is LOVE! Please tell somebody.

GeneralRe: Your book looks great - I need to order it ASAP PinmemberJack JH Xu23 Apr '07 - 9:33 
Hi, Paul,
 
Sorry to hear that. I contacted to the publisher and they told me that the first run of the book copies were sold out in few days mainly due to the bulk purchases from book resellers. Now they got second run out a week ago, so the book is available in stock. They also told me that they have completed all orders for amazon except for orders placed after April 18. They don't know why amazon takes so long to ship your book. You need to ckeck with amazon again because this is not due to the publisher for the delay.

Hope you can get your book soon.
 
Jack
 
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#.

GeneralRe: Your book looks great - I need to order it ASAP PinmemberMichael Sync1 Aug '07 - 21:50 
I got this from the link you posted.
 
Not Found
The requested URL /~jack_xu/TableContents.htm was not found on this server.
 
Additionally, a 404 Not Found error was encountered while trying to use an ErrorDocument to handle the request.
Apache/1.3.37 Server at authors.unicadpublish.com Port 80
 
Thanks and Regards,
Michael Sync ( Blog: http://michaelsync.net)
 
If you want to thank me for my help, please vote my message by clicking one of numbers beside "Rate this message". Why vote? Plz Read it here. Thank you. Smile | :)

GeneralRe: Your book looks great - I need to order it ASAP PinmemberEric Engler6 Aug '07 - 5:19 
The URL changed:
 
http://authors.unicadpublish.com/~jack_xu/[^]

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Permalink | Advertise | Privacy | Mobile
Web02 | 2.6.130516.1 | Last Updated 7 Dec 2007
Article Copyright 2007 by Jack J. H. Xu
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid