GDI+ in Windows Forms Application

In Windows Forms you might want to create some shapes and other graphical objects. For that you can use the graphic elements of Windows Forms that helps to create a visual application. In Windows Forms GDI+ is used to create graphics, draw text and manipulate graphical images.

The core of GDI+ functionality is the Graphics class. GDI+ has a set of base classes that can be used to do custom drawing on the screen. The set of such classes are collectively called the GDI+ managed class interfaces. This class is the part of .NET Framework. It provides an environment for creating, deploying and running XML web services and other applications.

The GDI+ managed class interfaces consist of about 60 classes, 50 enumerations and 8 structures. You can group these class into various namespaces. Some of them are:

  1. System.Drawing
  2. System.Drawing.Drawing.2D
  3. System.Drawing.Imaging
  4. System.Drawing.Printing
  5. System.Drawing.Text
  6. System.Drawing.Design

The following describes the simple Windows Forms application.

Make your design page like this:

button

Here in this case when the user clicks on the first button it will get the shape that we made in the code behind file using GDI.

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.ComponentModel;  
  4. using System.Data;  
  5. using System.Drawing;  
  6. using System.Linq;  
  7. using System.Text;  
  8. using System.Windows.Forms;  
  9.   
  10. namespace WindowsFormsApplication1  
  11. {  
  12. publicpartialclassForm1 : Form  
  13.     {  
  14. public Form1()  
  15.         {  
  16.             InitializeComponent();  
  17.         }  
  18.   
  19. privatevoid button1_Click(object sender, EventArgs e)  
  20.         {  
  21. Graphics g1 = CreateGraphics();  
  22. Pen p = newPen(Color.Blue, 2);  
  23.             g1.DrawEllipse(p, 150, 20, 50, 50);  
  24.             g1.DrawEllipse(p, 160, 35, 10, 10);  
  25.             g1.DrawEllipse(p, 180, 35, 10, 10);  
  26.             g1.DrawArc(p, 150, 35, 50, 30, 45, 90);  
  27.             g1.DrawRectangle(p, 150, 75, 50, 100);  
  28.             g1.DrawLine(p, 175, 175, 150, 250);  
  29.             g1.DrawLine(p, 175, 175,200 , 250);  
  30.   
  31.         }  
  32.   
  33. privatevoid button2_Click(object sender, EventArgs e)  
  34.         {  
  35. Graphics g2 = CreateGraphics();  
  36. SolidBrush sbr = newSolidBrush(Color.Chocolate);  
  37.             g2.FillEllipse(sbr, 150, 25, 50, 50);  
  38.             sbr.Color = Color.Gray;  
  39.             g2.FillEllipse(sbr, 160, 35, 10, 10);  
  40.             g2.FillEllipse(sbr, 180, 35, 10, 10);  
  41.             sbr.Color = Color.Black;  
  42.             g2.FillPie(sbr, 150, 35, 50, 30, 45, 90);  
  43.             sbr.Color = Color.Bisque;  
  44.             g2.FillRectangle(sbr, 150, 75, 50, 100);  
  45.   
  46.   
  47.         }  
  48.     }  
  49. }  
Output

form

I hope you like it. Thank you for reading. Have a good day.