Re: Performance & new Pen() / new Brush()

Tech-Archive recommends: Fix windows errors by optimizing your registry



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


.



Relevant Pages

  • Re: Performance & new Pen() / new Brush()
    ... Generally what I do is define a "Palette" object that contains the ... Pens & Brushes that my drawing code needs, ... Private ReadOnly m_background As Brush ... Public Sub Dispose() Implements IDisposable.Dispose ...
    (microsoft.public.dotnet.framework.drawing)
  • Re: GIMP on a small screen
    ... Paint Shop Pro 8 on my Windows XP laptop. ... I've already gotten rid of all the brushes I'm never going to ... detach the brushes palette and hide it, then click on the current brush ... If you then select your brushes from the tool option palette/dialog (By default below the Gimp toolbox), you won't need to open up the brushes palette. ...
    (comp.graphics.apps.gimp)