Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Drawing a discontinuous line with pen width > 1 fails with "Out of memory" error

53 views
Skip to first unread message

p...@twave.net

unread,
Oct 6, 2008, 4:01:54 PM10/6/08
to
I want to make some "smudges" on a form by dragging the mouse with the
left button down, then stop drawing when the mouse button is up. The
following code works fine when the variable sglPenWidth below is set
to one, but when it is 2 or more I get an OutOfMemoryException as soon
as I start the second drag. Code is below, and the details of the
exception are below that. If I take the Invalidate out of the
MouseMove event and put it on a button on the form, it doesn't crash.
But it doesn't draw the smudge as it is being created, which doesn't
work for me. If I Invalidate with a Timer it's in between - I get
through several MouseUp events but then it crashes with the
OutOfMemoryException.

Corrections *or* a workaround would be appreciated.

Public Class Form1
Dim gpTest As New System.Drawing.Drawing2D.GraphicsPath
Dim intAlpha As Integer
Dim sglPenWidth As Integer
Dim clrSchmutz As Color
Dim penSchmutz As Pen

Private Sub Form1_MouseMove(ByVal sender As Object, ByVal e As
System.Windows.Forms.MouseEventArgs) Handles Me.MouseMove
If e.Button = Windows.Forms.MouseButtons.Left Then
gpTest.AddLine(e.X, e.Y, e.X, e.Y)
Me.Invalidate()
End If
End Sub

Private Sub Form1_MouseUp(ByVal sender As Object, ByVal e As
System.Windows.Forms.MouseEventArgs) Handles Me.MouseUp
gpTest.StartFigure()
End Sub

Private Sub Form1_Paint(ByVal sender As Object, ByVal e As
System.Windows.Forms.PaintEventArgs) Handles Me.Paint
e.Graphics.DrawPath(penSchmutz, gpTest) 'error occurs here
at the start of the 2nd mouse drag
End Sub

Public Sub New()
' This call is required by the Windows Form Designer.
InitializeComponent()

' Add any initialization after the InitializeComponent() call.
intAlpha = 100
sglPenWidth = 2
clrSchmutz = System.Drawing.Color.Black
penSchmutz = New Pen(Color.FromArgb(intAlpha, clrSchmutz),
sglPenWidth)
End Sub
End Class

System.OutOfMemoryException was unhandled
Message="Out of memory."
Source="System.Drawing"
StackTrace:
at System.Drawing.Graphics.CheckErrorStatus(Int32 status) at
System.Drawing.Graphics.DrawPath(Pen pen, GraphicsPath path) at
TestGraphics.Form1.Form1_Paint(Object sender, PaintEventArgs e) in C:
\Documents and Settings\Visual Studio Projects\TestGraphics
\TestGraphics\Form1.vb:line 20 at
System.Windows.Forms.Control.OnPaint(PaintEventArgs e) at
System.Windows.Forms.Form.OnPaint(PaintEventArgs e) at
System.Windows.Forms.Control.PaintWithErrorHandling(PaintEventArgs e,
Int16 layer, Boolean disposeEventArgs) at
System.Windows.Forms.Control.WmPaint(Message& m) at
System.Windows.Forms.Control.WndProc(Message& m) at
System.Windows.Forms.ScrollableControl.WndProc(Message& m) at
System.Windows.Forms.ContainerControl.WndProc(Message& m) at
System.Windows.Forms.Form.WndProc(Message& m) at
System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message&
m) at
System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message&
m) at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr
hWnd, Int32 msg, IntPtr wparam, IntPtr lparam) at
System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
at
System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(Int32
dwComponentID, Int32 reason, Int32 pvLoopData) at
System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32
reason, ApplicationContext context) at
System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32
reason, ApplicationContext context) at
System.Windows.Forms.Application.Run(ApplicationContext context) at
Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.OnRun()
at
Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.DoApplicationModel()
at
Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.Run(String[]
commandLine) at TestGraphics.My.MyApplication.Main(String[] Args)
in 17d14f5c-a337-4978-8281-53493378c1071.vb:line 81 at
System.AppDomain._nExecuteAssembly(Assembly assembly, String[]
args) at System.AppDomain.ExecuteAssembly(String assemblyFile,
Evidence assemblySecurity, String[] args) at
Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.Run(ExecutionContext
executionContext, ContextCallback callback, Object state) at
System.Threading.ThreadHelper.ThreadStart()
InnerException:

Mick Doherty

unread,
Oct 6, 2008, 6:12:44 PM10/6/08
to
Looks like a bug to me.

It's the Pens DashPattern which is causing an out of memory exception.
Simply give it a value and no more errors.

penSchmutz.DashPattern = New Single() {1.0F}

However, your Pen now has a DashPattern which you may not want.

--
Mick Doherty
http://dotnetrix.co.uk/nothing.htm

<p...@twave.net> wrote in message
news:66fe7668-a6d8-4e6b...@64g2000hsu.googlegroups.com...

p...@twave.net

unread,
Oct 7, 2008, 7:30:30 AM10/7/08
to
On Oct 6, 6:12 pm, "Mick Doherty"
<EXCHANGE#W...@AND.REMOVE.SQUAREBRACKETS.
[mick.doherty#dotnetrix.co.uk]> wrote:

I like it! Looks like a good workaround to me.

How did you know it was the dash pattern?

Peter Bradshaw
p...@twave.net

> Looks like a bug to me.
>
> It's the Pens DashPattern which is causing an out of memory exception.
> Simply give it a value and no more errors.
>
> penSchmutz.DashPattern = New Single() {1.0F}
>
> However, your Pen now has a DashPattern which you may not want.
>
> --

> Mick Dohertyhttp://dotnetrix.co.uk/nothing.htm

> > System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.Unsa­feNativeMethods.IMsoComponentManager.FPushMessageLoop(Int32


> > dwComponentID, Int32 reason, Int32 pvLoopData)    at
> > System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32
> > reason, ApplicationContext context)    at
> > System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32
> > reason, ApplicationContext context)    at
> > System.Windows.Forms.Application.Run(ApplicationContext context)    at

> > Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.OnRun­()
> > at
> > Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.DoApp­licationModel()
> > at
> > Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.Run(S­tring[]


> > commandLine)    at TestGraphics.My.MyApplication.Main(String[] Args)
> > in 17d14f5c-a337-4978-8281-53493378c1071.vb:line 81    at
> > System.AppDomain._nExecuteAssembly(Assembly assembly, String[]
> > args)    at System.AppDomain.ExecuteAssembly(String assemblyFile,
> > Evidence assemblySecurity, String[] args)    at
> > Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
> > at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
> > at System.Threading.ExecutionContext.Run(ExecutionContext
> > executionContext, ContextCallback callback, Object state)    at
> > System.Threading.ThreadHelper.ThreadStart()

> >  InnerException:- Hide quoted text -
>
> - Show quoted text -

Mick Doherty

unread,
Oct 10, 2008, 7:25:00 AM10/10/08
to
Hi Peter,

I just pasted your code into a new project and ran it. When it failed I
hovered the mouse over the penShmutz argument in the DrawPath() call ( which
was automatically highlighted by VS). This allowed me to inspect the pen and
after expanding to the Properties view I could see the DashPattern property
had an "{Out of memory}" value. I then just gave it a value and problem
solved.

--
Mick Doherty
http://dotnetrix.co.uk/nothing.htm

<p...@twave.net> wrote in message
news:b6010ea2-54bf-4186...@d45g2000hsc.googlegroups.com...

p...@twave.net

unread,
Oct 12, 2008, 9:10:59 AM10/12/08
to
On Oct 10, 7:25 am, "Mick Doherty"

<EXCHANGE#W...@AND.REMOVE.SQUAREBRACKETS.
[mick.doherty#dotnetrix.co.uk]> wrote:
> Hi Peter,
>
> I just pasted your code into a new project and ran it. When it failed I
> hovered the mouse over the penShmutz argument in the DrawPath() call ( which
> was automatically highlighted by VS). This allowed me to inspect the pen and
> after expanding to the Properties view I could see the DashPattern property
> had an "{Out of memory}" value. I then just gave it a value and problem
> solved.
>
> --
> Mick Dohertyhttp://dotnetrix.co.uk/nothing.htm

>
> <p...@twave.net> wrote in message
>
> news:b6010ea2-54bf-4186...@d45g2000hsc.googlegroups.com...
>
>
>
> > On Oct 6, 6:12 pm, "Mick Doherty"
> > <EXCHANGE#W...@AND.REMOVE.SQUAREBRACKETS.
> > [mick.doherty#dotnetrix.co.uk]> wrote:
>
> > I like it! Looks like a good workaround to me.
>
> > How did you know it was the dash pattern?
>
> > Peter Bradshaw
> > p...@twave.net- Hide quoted text -

>
> - Show quoted text -

Hello Mick,

Well now I'm *glad* I had that little error that stymied me for two
weeks. At this stage one of the biggest reasons I write programs is to
get experience in debugging them. I had assumed the error was in the
DrawPath method and had hovered over and expanded that, to no benefit.
It never occurred to me to interrogate one of the arguments.

Thanks again.

Peter Bradshaw
p...@twave.net

0 new messages