Loading...

Use of Graphic Class in C# to make Circle, Triangle and Square with Color Change Dialog...



Graphic Class to make Different Shapes:

We use Graphic Class in C# to make different shapes. In this method we make a object of graphics class and use it.
Before you can draw lines and shapes, render text, or display and manipulate images with GDI+, you need to create a Graphics object. The Graphics object represents a GDI+ drawing surface, and is the object that is used to create graphical images.
There are two steps in working with graphics:
  1. Creating a Graphics object.
  2. Using the Graphics object to draw lines and shapes, render text, or display and manipulate images. ~ According to msdn.microsoft.com

 Making Object:


   Graphics  graphic = panel1.CreateGraphics();    // Suppose you want to Make shapes on a Panel..
  • If you want to make on form you will just write:
      Graphics  graphic = CreateGraphics();
          Pen pen = new Pen(Color.Black, 2);

To Make Circle using Graphics in C#:


           graphic.DrawEllipse(pen,x,y,w,h);            // x is x axis point , y is y axis point... w is width of circle and h is height of circle


To Make Square using Graphics in C#:


           graphic.DrawRectangle(pen, x, y, w, h);                // same as above .. Circle comment

To Make Triangle using Graphics in C#:


           graphic.DrawPolygon(pen, new PointF[] {new Point(x,y),new Point(x-(size/2),y+size),new Point(x+(size/2),y+size) });                                         // logic is to make simple triangle.. u will understand when you will run the code...


To Change Color of Pen using Graphics in C#:


     private void btn_clr_Click(object sender, EventArgs e) // event of clicking on change color button
           {
                ColorDialog  clrd = new ColorDialog();  // Creating Object
                DialogResult result = clrd.ShowDialog();  // Checking Result of Dialog
                if(result == System.Windows.Forms.DialogResult.OK)  // Condition if User click OK
                {
                    pen = new Pen(clrd.Color, 2);  // Color Changed here
                }
           
           }

Output:




Hope You Understand... If you didn't understand, just Comment here we will teach you in the more easy steps...


Commenting on a post is a great way to show some love for what you're seeing.

Google+

 
TOP