Re: Performance & new Pen() / new Brush()
- From: "Jay B. Harlow [MVP - Outlook]" <Jay_Harlow_MVP@xxxxxxxxxxxxx>
- Date: Thu, 15 Jun 2006 07:09:36 -0500
Lloyd,
Do the pens & brushes tend to be the same color?
Creating 300 red brushes seems wasteful.
Generally what I do is define a "Palette" object that contains the distinct
Pens & Brushes that my drawing code needs, I create & dispose this "Palette"
object in the OnPaint event. The "Palette" object is responsible for
disposing of the contained.
Something like:
Public Class Palette
Implements IDisposable
Private ReadOnly m_background As Brush
Private ReadOnly m_border As Pen
Public Sub New()
m_background = New SolidBrush(Color.Red)
m_border = New Pen(Color.Blue)
End Sub
Public ReadOnly Property Background() As Brush
Get
Return m_background
End Get
End Property
Public ReadOnly Property Border() As Pen
Get
Return m_border
End Get
End Property
Public Sub Dispose() Implements IDisposable.Dispose
m_background.Dispose()
m_border.Dispose()
End Sub
End Class
Protected Overrides Sub OnPaint(ByVal e As PaintEventArgs)
MyBase.OnPaint(e)
Using palette As New Palette
e.Graphics.FillRectangle(palette.Background, ClientRectangle)
e.Graphics.DrawRectangle(palette.Border, ClientRectangle)
End Using
End Sub
Note, sometimes I refactor my code so the Palette object does the drawing,
in which case I rename the Palette to Renderer.
Public Class Renderer
Inherits Palette
Public Sub DrawBorder(ByVal gr As Graphics, ByVal rect As Rectangle)
gr.DrawRectangle(Border, rect)
End Sub
End Class
Protected Overrides Sub OnPaint(ByVal e As PaintEventArgs)
MyBase.OnPaint(e)
Using renderer As New Renderer
e.Graphics.FillRectangle(palette.Background, ClientRectangle)
palette.DrawBorder(e.Graphics, ClientRectangle)
End Using
End Sub
NOTE: I don't do the above for performance as simple organization & to
reduce "code smells". Although I have never timed it I would expect some
performance improvements as its reducing the number of objects created,
which should reduce the amount of GC pressure.
--
Hope this helps
Jay B. Harlow [MVP - Outlook]
..NET Application Architect, Enthusiast, & Evangelist
T.S. Bradley - http://www.tsbradley.net
"Lloyd Dupont" <net.galador@ld> wrote in message
news:u1IYpQEkGHA.4444@xxxxxxxxxxxxxxxxxxxxxxx
| In my drawing code I recusrively create an average number of pens and
| brushes of, let's guess: 300?
| Would I see performance improvment by just caching those?
|
| (actually I use and create all of them in a using() statment)
|
| --
| Regards,
| Lloyd Dupont
|
| NovaMind development team
| NovaMind Software
| Mind Mapping Software
| <www.nova-mind.com>
|
|
.
- Follow-Ups:
- Re: Performance & new Pen() / new Brush()
- From: Lloyd Dupont
- Re: Performance & new Pen() / new Brush()
- From: Jay B. Harlow [MVP - Outlook]
- Re: Performance & new Pen() / new Brush()
- References:
- Performance & new Pen() / new Brush()
- From: Lloyd Dupont
- Performance & new Pen() / new Brush()
- Prev by Date: Re: Performance & new Pen() / new Brush()
- Next by Date: Re: Performance & new Pen() / new Brush()
- Previous by thread: Re: Performance & new Pen() / new Brush()
- Next by thread: Re: Performance & new Pen() / new Brush()
- Index(es):
Relevant Pages
|