title: Graphics.DrawLine() Always Throw the OutofMemoryException in Some Condition
body: <p>Here are my codes.
  A very simple method that handles the System.Windows.Forms.Form.Paint event.</p>
  
  <pre><code>void Form1_Paint(object sender, PaintEventArgs e)
  {
      e.Graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
      PointF start = new PointF(121.0F, 106.329636F);
      PointF end = new PointF(0.9999999F, 106.329613F);
  
      using (Pen p05 = new Pen(Color.Red, 1F))
      {
          p05.DashStyle = System.Drawing.Drawing2D.DashStyle.Custom;
          p05.DashPattern = new float[] { 4, 2, 1, 3 };
          e.Graphics.DrawLine(p05, start, end);
      }
  }
  </code></pre>
  
  <p>This method always throws the OutofMemoryException.</p>
  
  <p><strong>I have tested the code under WindowsXP32bit .Net Framework 2.0,3.0 and 3.5,and the result are the same.But it works fine under Windows732bit and Net Framework  2.0,3.0 and 3.5.</strong></p>
  
  <p>Here are what i do to stop the code from throwing the OutofMemoryException.(1 or 2 or 3)</p>
  
  <p>1.Set the Graphics.SmoothingMode to System.Drawing.Drawing2D.SmoothingMode.Default</p>
  
  <p>2.Change the end point to PointF(0.99999F, 106.329613F)</p>
  
  <p>3.Set the DashStyle to System.Drawing.Drawing2D.DashStyle.Solid</p>
  
  <p>But i do need the SmoothingMode and DashStyle.
  And in the real program the coordinate of the point to draw are always transformed by a Matrix which represents the movement ,rotation ,and scaling that applied to the underlaying image.So coordinate like (0.9999999F, 106.329613F) are possible.</p>
  
  <p>Could you please tell me how to handle this annoying problem.</p>
  
  <p>Best Regards,</p>
  
  <p><strong>STACK TRACE</strong> </p>
  
  <ul>
  <li><p>Oct 7th,2010 added</p>
  
  <p>System.OutOfMemoryException
    Message="Out Of Memory"
    Source="System.Drawing"
    StackTrace:
         location System.Drawing.Graphics.CheckErrorStatus(Int32 status)
         location System.Drawing.Graphics.DrawLine(Pen pen, Single x1, Single y1, Single x2, Single y2)
         location System.Drawing.Graphics.DrawLine(Pen pen, PointF pt1, PointF pt2)
         location WindowsFormsApplication1.Form1.Form1_Paint(Object sender, PaintEventArgs e) location C:\Documents and Settings\sunyi\My Documents\Visual Studio 2008\Projects\WindowsFormsApplication1\WindowsFormsApplication1\Form1.cs:line 24
         location System.Windows.Forms.Control.OnPaint(PaintEventArgs e)
         location System.Windows.Forms.Form.OnPaint(PaintEventArgs e)
         location System.Windows.Forms.Control.PaintWithErrorHandling(PaintEventArgs e, Int16 layer, Boolean disposeEventArgs)
         location System.Windows.Forms.Control.WmPaint(Message&amp; m)
         location System.Windows.Forms.Control.WndProc(Message&amp; m)
         location System.Windows.Forms.ScrollableControl.WndProc(Message&amp; m)
         location System.Windows.Forms.ContainerControl.WndProc(Message&amp; m)
         location System.Windows.Forms.Form.WndProc(Message&amp; m)
         location System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message&amp; m)
         location System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message&amp; m)
         location System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
         location System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG&amp; msg)
         location System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(Int32 dwComponentID, Int32 reason, Int32 pvLoopData)
         location System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
         location System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
         location System.Windows.Forms.Application.Run(Form mainForm)
         location WindowsFormsApplication1.Program.Main() location C:\Documents and Settings\sunyi\My Documents\Visual Studio 2008\Projects\WindowsFormsApplication1\WindowsFormsApplication1\Program.cs:line 17
         location System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args)
         location System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
         location Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
         location System.Threading.ThreadHelper.ThreadStart_Context(Object state)
         location System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
         location System.Threading.ThreadHelper.ThreadStart()
    InnerException:</p></li>
  </ul>
  
  <p><strong>FULL SOURCE</strong> </p>
  
  <ul>
  <li>Oct 7th,2010 added  </li>
  <li>Oct 7th,2010 updated according to vulkanino's advice</li>
  </ul>
  
  <p><em>Form1.designer.cs</em></p>
  
  <pre><code>namespace WindowsFormsApplication1
      {
          partial class Form1
          {
              private System.ComponentModel.IContainer components = null;
  
              protected override void Dispose(bool disposing)
              {
                  if (disposing &amp;&amp; (components != null))
                  {
                      components.Dispose();
                  }
                  base.Dispose(disposing);
              }
  
              private void InitializeComponent()
              {
                  this.SuspendLayout();
                  //
                  // Form1
                  //
                  this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
                  this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
                  this.ClientSize = new System.Drawing.Size(292, 266);
                  this.Name = "Form1";
                  this.Text = "Form1";
                  this.ResumeLayout(false);
              }
          }
      }
  </code></pre>
  
  <p><em>Form1.cs</em></p>
  
  <pre><code>using System.Drawing;
  using System.Windows.Forms;
  
  namespace WindowsFormsApplication1
  {
      public partial class Form1 : Form
      {
          public Form1()
          {
              InitializeComponent();
              p05 = new Pen(Color.Red, 1F);
              p05.DashStyle = System.Drawing.Drawing2D.DashStyle.Custom;
              p05.DashPattern = dashStyle;
              this.Paint += new PaintEventHandler(Form1_Paint);
          }
  
          void Form1_Paint(object sender, PaintEventArgs e)
          {
              e.Graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
              e.Graphics.DrawLine(p05, start, end);
          }
  
          // start and end point are within the
          // client rectangle of the form which has been specified in the designer (292, 266);
          private PointF start = new PointF(121.0F, 106.329636F);
          private PointF end = new PointF(0.9999999F, 106.329613F);
          private float[] dashStyle = new float[] { 4, 2, 1, 3 };
          private Pen p05;
      }
  }
  </code></pre>