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:
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#:
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:
Commenting on a post is a great way to show some love for what you're seeing.