tags:

views:

61

answers:

1

Here are my codes. A very simple method that handles the System.Windows.Forms.Form.Paint event.

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);
    }
}

This method always throws the OutofMemoryException.

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.

Here are what i do to stop the code from throwing the OutofMemoryException.(1 or 2 or 3)

1.Set the Graphics.SmoothingMode to System.Drawing.Drawing2D.SmoothingMode.Default

2.Change the end point to PointF(0.99999F, 106.329613F)

3.Set the DashStyle to System.Drawing.Drawing2D.DashStyle.Solid

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.

Could you please tell me how to handle this annoying problem.

Best Regards,

STACK TRACE

  • Oct 7th,2010 added

    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& m) location System.Windows.Forms.Control.WndProc(Message& m) location System.Windows.Forms.ScrollableControl.WndProc(Message& m) location System.Windows.Forms.ContainerControl.WndProc(Message& m) location System.Windows.Forms.Form.WndProc(Message& m) location System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m) location System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m) location System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam) location System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& 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:

FULL SOURCE

  • Oct 7th,2010 added
  • Oct 7th,2010 updated according to vulkanino's advice

Form1.designer.cs

namespace WindowsFormsApplication1
    {
        partial class Form1
        {
            private System.ComponentModel.IContainer components = null;

            protected override void Dispose(bool disposing)
            {
                if (disposing && (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);
            }
        }
    }

Form1.cs

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;
    }
}
A: 

It could really be an Out Of Memory error; since some of your data is constant, please try to put that data outside the Paint method, so you don't have to call New:

void Form1_Paint(object sender, PaintEventArgs e)
        {
            //e.Graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.Default;
            e.Graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;

            using ( redPen )
            {
                redPen.DashStyle = System.Drawing.Drawing2D.DashStyle.Solid;
                redPen.DashPattern = dashPattern;
                e.Graphics.DrawLine(redPen, startingPoint, endingPoint);
            }
        }

private PointF startingPoint = new PointF(121.0F, 106.329636F);
private PointF endingPoint = new PointF(0.9999999F, 106.329613F);
private Pen redPen = new Pen(Color.Red, 1F);
private float[] dashPattern = { 4, 2, 1, 3 };

Another thing to consider is the graphics bounds. Before doing anything else, do:

  e.DrawBackground();
  Rectangle r = e.Bounds;

And then draw into the rectangle bounds.

edit: add the paint handler in the Load event, not in the constructor.

vulkanino
I have tried "move the Pen construction out of the Using block", but it doesn't solve the problem.
SunYi
OOM-Exception happen every time that i run it.
SunYi
You're also creating new points every time its called.
Jackson Pope
To Jackson Pope, I don't think this is a real OOM-Exception for it happens the first time that i call this method.
SunYi
To vulkanino, According to your advice, i updated my code (see the original post above). The problem is still there. And i couldn't find the method " DrawBackground(); " in PaintEventArgs.Do you mean PaintEventArgs.Graphics.SetClip(Rectangle)? It also doesn't work.
SunYi
I have tested the code under WindowsXP(32bit) .Net Framework 2.0,3.0 and 3.5,and the result are the same.But it works fine under Windows7(32bit) and Net Framework 2.0,3.0 and 3.5.
SunYi