title: Using System.Drawing.DrawLine to draw multiple lines/shapes from console application
body: <p>I am writing a program for my work that will take a custom directional code format and draw shapes based on the code given and save the result to an image.</p>
  <p>Example:</p>
  <blockquote>
  <p><strong>SQR=W10 S10 E10 N10$.</strong></p>
  </blockquote>
  <p>Would give an output of a 10x10 square shape on an image (<em>N, S, E, and W</em> are directions).</p>
  <p>I know how to get this done using <strong>OnPaint</strong> with a WPF application, but I need this to be done through a console application. I have all the data parsed and in classes and collections so all I have to do is draw. but I am having trouble getting past the memory issue; it only draws one line at a time and doesn't save the previous lines.</p>
  <p>The code I have partially drawing is:</p>
  <pre><code>Pen pen = new Pen(Color.Black, 2);
  Bitmap bmp = new Bitmap(xMax, yMax);
  
  using (var graphics = Graphics.FromImage(bmp))
  {
      graphics.FillRectangle(Brushes.White, new Rectangle(0, 0, xMax, yMax));
  
      foreach (Line line in lines)
      {
          graphics.DrawLine(pen, line.start, line.end);
      }
  }
  
  bmp.Save(&quot;test.jpg&quot;, System.Drawing.Imaging.ImageFormat.Jpeg);
  </code></pre>
  <p>Here is the code I have using <strong>DrawLines</strong>, instead:</p>
  <pre><code>Pen pen = new Pen(Color.Black, 2);
  Bitmap bmp = new Bitmap(xMax, yMax);
  
  using (var graphics = Graphics.FromImage(bmp))
  {
      graphics.FillRectangle(Brushes.White, new Rectangle(0, 0, xMax, yMax));
  
      Point[] pts = new Point[lines.Count];
      int i = 0;
  
      pts[0] = lines[0].start; // This sets the starting point.
  
      foreach (Line line in lines)
      {
          pts[i] = line.end;
          i++;
      }
  
      graphics.DrawLines(pen, pts);
  }
  
  bmp.Save(&quot;test.jpg&quot;, System.Drawing.Imaging.ImageFormat.Jpeg);
  </code></pre>
  <p>And here is my <strong>Line</strong> class:</p>
  <pre><code>class Line
  {
      public Point start { get; set; }
      public Point end { get; set; }
  }
  </code></pre>
  <p>This is what my List of points looks like right before drawing:</p>
  <p><a href="https://i.stack.imgur.com/Bs4jT.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/Bs4jT.png" alt="enter image description here" /></a></p>
  <h3>What are my options for doing this, using System.Drawing and a Console Application, and allowing it to save all lines to the resulting image?</h3>