Home

GDI+ Texture Brushes

 

School Enrolment

Introduction

This is an example of drawing a pie chart in GDI+.

Windows Controls:

  • Label
  • Button
  • Text Box
  • Picture Box

Practical LearningPractical Learning: Using Texture Brushes

  1. Start a new Windows Forms Application named SchoolEnrolment1
  2. Design the form as follows:
     
    School Enrolment
    Control Name Text
    Label Label   Enrolment / Program ___________________________
    Label Label   Graduates
    Label Label   Undergraduates
    Label Label   Certificates
    TextBox TextBox txtGraduates 0
    TextBox TextBox txtUndergraduates 0
    TextBox TextBox txtCertificates 0
    Button Button btnCreateChart Create Chart
    PictureBox PictureBox pbxChart  
    Label Label   ____Legend____
    Label Label lblGraduates Graduates
    Label Label lblUndergraduates Undergraduates
    Label Label lblCertificates Certificates
    Button Button btnClose Close
  3. To design a bitmap, on the main menu, click Project -> Add New Item...
  4. In the Templates list, click Bitmap File
  5. Change the Name to graduates and click Add
  6. To change the file location of the bitmap, on the main menu, click File -> Save graduates.bmp As...
  7. Locate and display the SchoolEnrolment2\SchoolEnrolment2\bin\debug folder
  8. Click Save
  9. In the Solution Explorer, double-click graduates.bmp to make sure it is displayed
  10. In the Properties Window, click Width and type 16
  11. Click Height and type 16
  12. Design the bitmap as follows:
     
    Graduates
  13. In the Solution Explorer, right-click the Debug folder -> Add -> New Item...
  14. In the Templates list, make sure Bitmap File is selected.
    Set the Name to undergraduates and click Add
  15. In the Properties window, click Width and type 16
  16. Click Height and type 16
  17. Design the bitmap as follows:
     
    Undergraduates
  18. In the Solution Explorer, right-click the Debug folder -> Add -> New Item...
  19. In the Templates list, make sure Bitmap File is selected.
    Set the Name to certificates and click Add
  20. In the Properties window, click Width and type 16
  21. Click Height and type 16
  22. Design the bitmap as follows:
     
    Certificates
  23. Display the form
  24. Right-click the form and click View Code
  25. Declare three variables as follows:
     
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;
    
    namespace SchoolEnrolment2
    {
        public partial class Form1 : Form
        {
            float Graduates;
            float Undergraduates;
            float Certificates;
    
            public Form1()
            {
                InitializeComponent();
            }
        }
    }
  26. Return to the form and click an unoccupied area of its body. In the Properties window, click the Events button Events
  27. Double-click the Paint field and implement the event as follows:
     
    private void Form1_Paint(object sender, PaintEventArgs e)
    {
        Bitmap bmpGraduates = new Bitmap("graduates.bmp");
        TextureBrush brushGraduates = new TextureBrush(bmpGraduates);
        Bitmap bmpUndergraduates = new Bitmap("undergraduates.bmp");
        TextureBrush brushUndergraduates = new TextureBrush(bmpUndergraduates);
        Bitmap bmpCertificates = new Bitmap("certificates.bmp");
        TextureBrush brushCertificates = new TextureBrush(bmpCertificates);
    
        pbxChart.CreateGraphics().FillPie(brushGraduates,
                                          0.0F,
                                          0.0F,
                                          260.0F,
                                          200.0F, 0.0F, Graduates);
    
        pbxChart.CreateGraphics().FillPie(brushUndergraduates,
                                          0.0F,
                                          0.0F,
                                          260.0F,
                                          200.0F, Graduates, Undergraduates);
    
        pbxChart.CreateGraphics().FillPie(brushCertificates,
                                          0.0F,
                                          0.0F,
                                          260.0F,
                                          200.0F, Graduates + Undergraduates,
                                          Certificates);
    
        e.Graphics.FillRectangle(brushGraduates,
                                 new Rectangle(lblGraduates.Left,
                                 lblGraduates.Top + 18,
                                 btnClose.Width,
                                 20));
    
        e.Graphics.DrawRectangle(new Pen(Color.Black),
                                 new Rectangle(lblGraduates.Left - 1,
                                 lblGraduates.Top + 18,
                                 btnClose.Width,
                                 20));
    
        e.Graphics.FillRectangle(brushUndergraduates,
                                      new Rectangle(btnClose.Left,
                                    lblUndergraduates.Top + 18,
                            btnClose.Width,
                            20));
    
                e.Graphics.DrawRectangle(new Pen(Color.Black),
                                      new Rectangle(btnClose.Left - 1,
                                    lblUndergraduates.Top + 18,
                            btnClose.Width + 1,
                            20));
    
                e.Graphics.FillRectangle(brushCertificates,
                                      new Rectangle(btnClose.Left,
                                    lblCertificates.Top + 18,
                            btnClose.Width,
                                20));
    
                e.Graphics.DrawRectangle(new Pen(Color.Black),
                                      new Rectangle(btnClose.Left - 1,
                                    lblCertificates.Top + 18,
                            btnClose.Width + 1,
                                20));
    
    
                pbxChart.CreateGraphics().DrawPie(new Pen(Color.Blue),
                                          0.0F,
                                  0.0F,
                                  260.0F,
                                  200.0F, 0.0F, Graduates);
                pbxChart.CreateGraphics().DrawPie(new Pen(Color.Red),
                                          0.0F,
                                  0.0F,
                                  260.0F,
                            200.0F, Graduates, Undergraduates);
                pbxChart.CreateGraphics().DrawPie(new Pen(Color.Green),
                                          0.0F,
                                  0.0F,
                                  260.0F,
                              200.0F, Graduates + Undergraduates,
                                  Certificates);
    
                pbxChart.CreateGraphics().DrawEllipse(new Pen(Color.Red, 2),
                                          new Rectangle(0,
                                            0,
                                        260,
                                        200));
    }
  28. Return to the form and click the picture box
  29. In the Events section of the Properties window, double-click Paint and implement its event as follows:
     
    private void pbxChart_Paint(object sender, PaintEventArgs e)
    {
                Invalidate();
    }
  30. Return to the form and double-click the Create Chart button
  31. Implement the event as follows:
     
    private void btnCreateChart_Click(object sender, EventArgs e)
    {
                float grad = 0.00F,
                      under = 0.00F,
                      cert = 0.00F,
                      total = 0.00F;
                float percentGraduates = 0.00F,
                      percentUndergraduates = 0.00F,
                      percentCertificates = 0.00F;
    
                try
                {
                    grad = float.Parse(txtGraduates.Text);
                }
                catch (FormatException)
                {
                    MessageBox.Show("Invalid Value");
                }
                try
                {
                    under = float.Parse(txtUndergraduates.Text);
                }
                catch (FormatException)
                {
                    MessageBox.Show("Invalid Value");
                }
    
                try
                {
                    cert = float.Parse(txtCertificates.Text);
                }
                catch (FormatException)
                {
                    MessageBox.Show("Invalid Value");
                }
    
                total = grad + under + cert;
                percentGraduates = (grad / total) * 100;
                percentUndergraduates = (under / total) * 100;
                percentCertificates = (cert / total) * 100;
    
                Graduates = (360 * percentGraduates) / 100;
                Undergraduates = (360 * percentUndergraduates) / 100;
                Certificates = (360 * percentCertificates) / 100;
    
                pbxChart.Invalidate();
    }
  32. Return to the form and double-click the Close button
  33. Implement its event as follows:
     
    private void btnClose_Click(object sender, EventArgs e)
    {
                Close();
    }
  34. Execute the application and test the form
     
     School Enrolment
  35. After using it, close the form
 

Home Copyright © 2007 FunctionX, Inc.