@@ -45,6 +45,11 @@ public class GraphicsRenderContext : RenderContextBase, IDisposable
4545 /// </summary>
4646 private readonly Dictionary < OxyColor , Brush > brushes = new Dictionary < OxyColor , Brush > ( ) ;
4747
48+ /// <summary>
49+ /// The pen cache.
50+ /// </summary>
51+ private readonly Dictionary < GraphicsPenDescription , Pen > pens = new Dictionary < GraphicsPenDescription , Pen > ( ) ;
52+
4853 /// <summary>
4954 /// The string format.
5055 /// </summary>
@@ -105,12 +110,10 @@ public override void DrawEllipse(OxyRect rect, OxyColor fill, OxyColor stroke, d
105110 {
106111 return ;
107112 }
108-
109- using ( var pen = this . CreatePen ( stroke , thickness ) )
110- {
111- this . g . SmoothingMode = SmoothingMode . HighQuality ;
112- this . g . DrawEllipse ( pen , ( float ) rect . Left , ( float ) rect . Top , ( float ) rect . Width , ( float ) rect . Height ) ;
113- }
113+
114+ var pen = this . GetCachedPen ( stroke , thickness ) ;
115+ this . g . SmoothingMode = SmoothingMode . HighQuality ;
116+ this . g . DrawEllipse ( pen , ( float ) rect . Left , ( float ) rect . Top , ( float ) rect . Width , ( float ) rect . Height ) ;
114117 }
115118
116119 /// <summary>
@@ -136,10 +139,8 @@ public override void DrawLine(
136139 }
137140
138141 this . g . SmoothingMode = aliased ? SmoothingMode . None : SmoothingMode . HighQuality ;
139- using ( var pen = this . CreatePen ( stroke , thickness , dashArray , lineJoin ) )
140- {
141- this . g . DrawLines ( pen , this . ToPoints ( points ) ) ;
142- }
142+ var pen = this . GetCachedPen ( stroke , thickness , dashArray , lineJoin ) ;
143+ this . g . DrawLines ( pen , this . ToPoints ( points ) ) ;
143144 }
144145
145146 /// <summary>
@@ -171,35 +172,16 @@ public override void DrawPolygon(
171172 var pts = this . ToPoints ( points ) ;
172173 if ( fill . IsVisible ( ) )
173174 {
174- this . g . FillPolygon ( fill . ToBrush ( ) , pts ) ;
175+ this . g . FillPolygon ( this . GetCachedBrush ( fill ) , pts ) ;
175176 }
176177
177178 if ( stroke . IsInvisible ( ) || thickness <= 0 )
178179 {
179180 return ;
180181 }
181182
182- using ( var pen = this . CreatePen ( stroke , thickness ) )
183- {
184- if ( dashArray != null )
185- {
186- pen . DashPattern = this . ToFloatArray ( dashArray ) ;
187- }
188-
189- switch ( lineJoin )
190- {
191- case OxyPlot . LineJoin . Round :
192- pen . LineJoin = System . Drawing . Drawing2D . LineJoin . Round ;
193- break ;
194- case OxyPlot . LineJoin . Bevel :
195- pen . LineJoin = System . Drawing . Drawing2D . LineJoin . Bevel ;
196- break ;
197-
198- // The default LineJoin is Miter
199- }
200-
201- this . g . DrawPolygon ( pen , pts ) ;
202- }
183+ var pen = this . GetCachedPen ( stroke , thickness , dashArray , lineJoin ) ;
184+ this . g . DrawPolygon ( pen , pts ) ;
203185 }
204186
205187 /// <summary>
@@ -214,18 +196,16 @@ public override void DrawRectangle(OxyRect rect, OxyColor fill, OxyColor stroke,
214196 if ( fill . IsVisible ( ) )
215197 {
216198 this . g . FillRectangle (
217- fill . ToBrush ( ) , ( float ) rect . Left , ( float ) rect . Top , ( float ) rect . Width , ( float ) rect . Height ) ;
199+ this . GetCachedBrush ( fill ) , ( float ) rect . Left , ( float ) rect . Top , ( float ) rect . Width , ( float ) rect . Height ) ;
218200 }
219201
220202 if ( stroke . IsInvisible ( ) || thickness <= 0 )
221203 {
222204 return ;
223205 }
224206
225- using ( var pen = this . CreatePen ( stroke , thickness ) )
226- {
227- this . g . DrawRectangle ( pen , ( float ) rect . Left , ( float ) rect . Top , ( float ) rect . Width , ( float ) rect . Height ) ;
228- }
207+ var pen = this . GetCachedPen ( stroke , thickness ) ;
208+ this . g . DrawRectangle ( pen , ( float ) rect . Left , ( float ) rect . Top , ( float ) rect . Width , ( float ) rect . Height ) ;
229209 }
230210
231211 /// <summary>
@@ -304,18 +284,18 @@ public override void DrawText(
304284 var graphicsState = this . g . Save ( ) ;
305285
306286 this . g . TranslateTransform ( ( float ) p . X , ( float ) p . Y ) ;
307-
287+
308288 var layoutRectangle = new RectangleF ( 0 , 0 , size . Width , size . Height ) ;
309289 if ( Math . Abs ( rotate ) > double . Epsilon )
310290 {
311291 this . g . RotateTransform ( ( float ) rotate ) ;
312-
292+
313293 layoutRectangle . Height += ( float ) ( fontSize / 18.0 ) ;
314294 }
315295
316296 this . g . TranslateTransform ( dx , dy ) ;
317297
318- this . g . DrawString ( text , font , fill . ToBrush ( ) , layoutRectangle , this . stringFormat ) ;
298+ this . g . DrawString ( text , font , this . GetCachedBrush ( fill ) , layoutRectangle , this . stringFormat ) ;
319299
320300 this . g . Restore ( graphicsState ) ;
321301 }
@@ -437,6 +417,7 @@ public void Dispose()
437417 {
438418 i . Value . Dispose ( ) ;
439419 }
420+ this . imageCache . Clear ( ) ;
440421
441422 // dispose pens, brushes etc.
442423 this . stringFormat . Dispose ( ) ;
@@ -445,6 +426,13 @@ public void Dispose()
445426 {
446427 brush . Dispose ( ) ;
447428 }
429+ this . brushes . Clear ( ) ;
430+
431+ foreach ( var pen in this . pens . Values )
432+ {
433+ pen . Dispose ( ) ;
434+ }
435+ this . pens . Clear ( ) ;
448436 }
449437
450438 /// <summary>
@@ -509,7 +497,28 @@ private Brush GetCachedBrush(OxyColor fill)
509497 }
510498
511499 /// <summary>
512- /// Gets a cached pen.
500+ /// Gets the cached pen.
501+ /// </summary>
502+ /// <param name="stroke">The stroke color.</param>
503+ /// <param name="thickness">The thickness.</param>
504+ /// <param name="dashArray">The dash array.</param>
505+ /// <param name="lineJoin">The line join.</param>
506+ /// <returns>A <see cref="Pen" />.</returns>
507+ private Pen GetCachedPen ( OxyColor stroke , double thickness , double [ ] dashArray = null , OxyPlot . LineJoin lineJoin = OxyPlot . LineJoin . Miter )
508+ {
509+ GraphicsPenDescription description = new GraphicsPenDescription ( stroke , thickness , dashArray , lineJoin ) ;
510+
511+ Pen pen ;
512+ if ( this . pens . TryGetValue ( description , out pen ) )
513+ {
514+ return pen ;
515+ }
516+
517+ return this . pens [ description ] = CreatePen ( stroke , thickness , dashArray , lineJoin ) ;
518+ }
519+
520+ /// <summary>
521+ /// Creates a pen.
513522 /// </summary>
514523 /// <param name="stroke">The stroke.</param>
515524 /// <param name="thickness">The thickness.</param>
@@ -519,6 +528,7 @@ private Brush GetCachedBrush(OxyColor fill)
519528 private Pen CreatePen ( OxyColor stroke , double thickness , double [ ] dashArray = null , OxyPlot . LineJoin lineJoin = OxyPlot . LineJoin . Miter )
520529 {
521530 var pen = new Pen ( stroke . ToColor ( ) , ( float ) thickness ) ;
531+
522532 if ( dashArray != null )
523533 {
524534 pen . DashPattern = this . ToFloatArray ( dashArray ) ;
@@ -532,7 +542,7 @@ private Pen CreatePen(OxyColor stroke, double thickness, double[] dashArray = nu
532542 case OxyPlot . LineJoin . Bevel :
533543 pen . LineJoin = System . Drawing . Drawing2D . LineJoin . Bevel ;
534544 break ;
535- //// The default LineJoin is Miter
545+ // The default LineJoin is Miter
536546 }
537547
538548 return pen ;
@@ -581,4 +591,4 @@ private PointF[] ToPoints(IList<ScreenPoint> points)
581591 return r ;
582592 }
583593 }
584- }
594+ }
0 commit comments