Mombu the Programming Forum

Go Back   Mombu the Programming Forum > Microsoft > Drawing a discontinuous line with pen width > 1 fails with "Out ofmemory" error
User Name
Password
REGISTER NOW! Mark Forums Read




Reply
 
1 5th February 22:12
phb
External User
 
Posts: 1
Default Drawing a discontinuous line with pen width > 1 fails with "Out ofmemory" error



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(PaintEventArg s e) at
System.Windows.Forms.Form.OnPaint(PaintEventArgs e) at
System.Windows.Forms.Control.PaintWithErrorHandlin g(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(Mes sage& m) at
System.Windows.Forms.ContainerControl.WndProc(Mess age& m) at
System.Windows.Forms.Form.WndProc(Message& m) at
System.Windows.Forms.Control.ControlNativeWindow.O nMessage(Message&
m) at
System.Windows.Forms.Control.ControlNativeWindow.W ndProc(Message&
m) at System.Windows.Forms.NativeWindow.DebuggableCallba ck(IntPtr
hWnd, Int32 msg, IntPtr wparam, IntPtr lparam) at
System.Windows.Forms.UnsafeNativeMethods.DispatchM essageW(MSG& msg)
at
System.Windows.Forms.Application.ComponentManager. System.Windows.Forms.UnsafeNativeMethods.IMsoCompo nentManager.FPushMessageLoop(Int32
dwComponentID, Int32 reason, Int32 pvLoopData) at
System.Windows.Forms.Application.ThreadContext.Run MessageLoopInner(Int32
reason, ApplicationContext context) at
System.Windows.Forms.Application.ThreadContext.Run MessageLoop(Int32
reason, ApplicationContext context) at
System.Windows.Forms.Application.Run(ApplicationCo ntext context) at
Microsoft.VisualBasic.ApplicationServices.WindowsF ormsApplicationBase.OnRun()
at
Microsoft.VisualBasic.ApplicationServices.WindowsF ormsApplicationBase.DoApplicationModel()
at
Microsoft.VisualBasic.ApplicationServices.WindowsF ormsApplicationBase.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.Run UsersAssembly()
at System.Threading.ThreadHelper.ThreadStart_Context( Object state)
at System.Threading.ExecutionContext.Run(ExecutionCon text
executionContext, ContextCallback callback, Object state) at
System.Threading.ThreadHelper.ThreadStart()
InnerException:
  Reply With Quote


 


2 5th February 22:12
mick doherty
External User
 
Posts: 1
Default Drawing a discontinuous line with pen width > 1 fails with "Out of memory" error



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
  Reply With Quote
3 5th February 22:12
phb
External User
 
Posts: 1
Default Drawing a discontinuous line with pen width > 1 fails with "Outof memory" error


On Oct 6, 6:12*pm, "Mick Doherty"
<EXCHANGE#W...@AND.REMOVE.SQUAREBRACKETS.


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

How did you know it was the dash pattern?
Peter Bradshaw phb@twave.net
  Reply With Quote
4 5th February 22:12
mick doherty
External User
 
Posts: 1
Default Drawing a discontinuous line with pen width > 1 fails with "Out of memory" error


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
  Reply With Quote
5 5th February 22:12
phb
External User
 
Posts: 1
Default Drawing a discontinuous line with pen width > 1 fails with "Outof memory" error


On Oct 10, 7:25*am, "Mick Doherty"
<EXCHANGE#W...@AND.REMOVE.SQUAREBRACKETS.


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
phb@twave.net
  Reply With Quote
Reply


Thread Tools
Display Modes




Copyright © 2006 SmartyDevil.com - Dies Mies Jeschet Boenedoesef Douvema Enitemaus -
666