Adagio
Well-known member
- Joined
- Dec 12, 2005
- Messages
- 162
- Programming Experience
- Beginner
I have a program that keeps getting System.OutOfMemoryExceptions. The program relies on some "heavy" GDI+ drawing
All my drawing code looks basically like this:
	
	
	
	
	
	
	
	
	
		
			
			
			
			
			
		
	
	
	
		
	
	
		
	
MyItems could be like 30-40 items. Everything that OnPaint uses is set at the top of the class, they are are never changed or disposed, they stay the same until the program closes (ok, I do dispose them when the form closes, but not before)
I tried adding the fonts, brushes, etc inside the OnPaint method and disposed them after each use, but that seems to slow down the OnPaint sub a bit, also after a very short time it would throw an exception (don't remember the exception, but it was thrown when disposing a brush)
I only use DrawString, DrawRectangle and FillRectangle, nothing else
Does anyone have any idea on how to stop this error from happening? It's not like there's no memory left, there's plenty
	
		
			
		
		
	
				
			All my drawing code looks basically like this:
			
				VB.NET:
			
		
		
		Public Class MyList
    Private fntLucida18 As New Font("Lucida Console", 18)
    Private whiteBrush As Brush = Brushes.White
    Private blackBrush As Brush = Brushes.Black
    Private blackPen As Pen = Pens.Black
    Protected Overrides Sub OnPaint(ByVal e As System.Windows.Forms.PaintEventArgs)
        MyBase.OnPaint(e)
        With e.Graphics
            For Each item In MyItems
                .FillRectangle(whiteBrush, x, y, width, height)
                .DrawRectangle(blackPen, x, y, width, height)
                .DrawString("Some text", fntLucida11Regular, blackBrush, x, y)
            Next
        End With
    End Sub
End ClassI tried adding the fonts, brushes, etc inside the OnPaint method and disposed them after each use, but that seems to slow down the OnPaint sub a bit, also after a very short time it would throw an exception (don't remember the exception, but it was thrown when disposing a brush)
I only use DrawString, DrawRectangle and FillRectangle, nothing else
Does anyone have any idea on how to stop this error from happening? It's not like there's no memory left, there's plenty
 
	 
 
		 
 
		 
 
		 
 
		