- This topic explains about how to fill polygon with the help drawing class in C# programming.
- Here we try to fill color inside the triangle shape picture.
System.Drawing NameSpace – How to fill polygon in C#
- The System.Drawing namespace provides access to GDI+ basic graphics functionality.
- More advanced functionality is provided in the System.Drawing.Drawing2D, System.Drawing.Imaging, and System.Drawing.Text namespaces.
- The Graphics class provides methods for drawing to the display device.
- Classes such as Rectangle and Point encapsulate GDI+ primitives.
- The Pen class is used to draw lines and curves, while classes derived from the abstract class Brush are used to fill the interiors of shapes.
Graphics Class – How to fill polygon in C#
- Graphics class encapsulates a GDI+ drawing surface. This class cannot be inherited.
- Syntax for graphics class
|
1 2 |
public sealed class Graphics : MarshalByRefObject, IDeviceContext, IDisposable |
Graphics.DrawPolygon Method – How to fill polygon in C#
- Draws a polygon defined by an array of Point structures.
- Graphics.DrawPolygon Method (Pen, Point()) – How to fill polygon using vb
- Draws a polygon defined by an array of Point structures.
- Syntax for draw polygon
|
1 2 3 4 |
public void DrawPolygon( Pen pen, Point[] points ) |
- Pen – Pen that determines the color, width, and style of the polygon.
- Point() – Array of Point structures that represent the vertices of the polygon
Graphics.DrawPolygon Method (Pen, PointF()) – How to fill polygon in C#
- Draws a polygon defined by an array of PointF structures.
- Syntax for draw polygon
|
1 2 3 4 |
public void DrawPolygon( Pen pen, PointF[] points ) |
- Pen – Pen that determines the color, width, and style of the polygon.
- PointF – Array of PointF structures that represent the vertices of the polygon.
Graphics.Fillpolygon Method – How to fill polygon in C#
- It fills the interior of a polygon defined by an array of points specified by Point structures.
How to fill polygon in C# - Complete Code
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; namespace testc { public partial class Form1 : Form { Graphics gr_graphics = default(Graphics); //need a pen for drawing and make it black Pen pen_draw = new Pen(Color.Black); public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { } private void Form1_Paint(object sender, PaintEventArgs e) { gr_graphics = e.Graphics; //Draw a triangle on the form. //first have to define an array of points. Point[] pnt = new Point[3]; pnt[0].X = 150; pnt[0].Y = 150; pnt[1].X = 150; pnt[1].Y = 200; pnt[2].X = 50; pnt[2].Y = 120; gr_graphics.DrawPolygon(pen_draw, pnt); //Do a filled triangle //First create a brush Brush brush = default(Brush); brush = new SolidBrush(Color.Blue); //Now relocate the points for the triangle. pnt[0].X += 100; pnt[1].X += 100; pnt[2].X += 100; gr_graphics.FillPolygon(brush, pnt); } } } |

How to fill polygon in C# – Final fill polygon
How to fill polygon in C#






