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:
|