Q:

How to obtain dynamic polygon coordinates (from external file) using c# and display the polygon shape on a picture box

I need to be able to obtain coordinates and draw a polygon based on the given coordinates.
private void pictureBox1_Paint(object sender, PaintEventArgs e)
        {
            System.Drawing.Graphics g;
            g = this.CreateGraphics();
            g = Graphics.FromImage(pictureBox1.Image);
            Pen p = new Pen(Color.Black, 5);
            Point[] a = { new Point(100, 100), new Point(200, 100), new 
            Point(300, 200), new Point(200, 200), new Point(100, 400) };
            g.DrawPolygon(p, a);
            g.Dispose();
            pictureBox1.Refresh();

This code is for drawing a simple polygon with predefined points, but i need to get the points dynamically from an existing file consisting of the points.

A:

  private void button1_Click(object sender, EventArgs e)
        {
            string line;

            List<PointF> polygonData = new List<PointF>();                          
            System.Drawing.Graphics g;

            //drawing on picture box
            g = Graphics.FromImage(pictureBox1.Image);                
            Pen p = new Pen(Color.Black, 5);

            //a loop to collect all points available in the file
            while ((line = file.ReadLine()) != null)                     
            {
                var tokens = line.Split(splitChars);

                lat = float.Parse(tokens[0].ToString());
                longi = float.Parse(tokens[1].ToString());              

                polygonData.Add(new PointF(lat, longi));

            }

            if (polygonData.Count > 1)
            {
                //draw polygon based on points parsed to the loop
                  g.DrawPolygon(p, polygonData.ToArray());

            }

