Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

[WPF] More aggressive graphics memory cleanup

  • Loading branch information...
commit c5d947b20c5c9a1b0b10fe0b6b6cda4afbc769ee 1 parent 357b547
@ermau ermau authored
View
19 Xwt.WPF/Xwt.WPFBackend/ContextBackendHandler.cs
@@ -205,7 +205,14 @@ public void StrokePreserve (object backend)
public void SetColor (object backend, Color color)
{
var c = (DrawingContext) backend;
- c.SetColor (color.ToDrawingColor ());
+
+ var dc = color.ToDrawingColor ();
+ if (c.Pen.Color != dc) {
+ c.Pen.Color = dc;
+
+ c.Brush.Dispose();
+ c.Brush = new SolidBrush (dc);
+ }
}
public void SetLineWidth (object backend, double width)
@@ -264,13 +271,13 @@ public void SetPattern (object backend, object p)
public void SetFont (object backend, Font font)
{
var c = (DrawingContext) backend;
+ c.Font.Dispose();
c.Font = font.ToDrawingFont ();
}
public void DrawTextLayout (object backend, TextLayout layout, double x, double y)
{
var c = (DrawingContext)backend;
- var sfont = layout.Font.ToDrawingFont ();
var measure = layout.GetSize ();
var h = layout.Height > 0 ? (float)layout.Height : (float)measure.Height;
var stringFormat = TextLayoutContext.StringFormat;
@@ -280,6 +287,7 @@ public void DrawTextLayout (object backend, TextLayout layout, double x, double
stringFormat = (System.Drawing.StringFormat)stringFormat.Clone ();
stringFormat.Trimming = sdStringFormat;
}
+
c.Graphics.DrawString (layout.Text, layout.Font.ToDrawingFont (), c.Brush,
new RectangleF ((float)x, (float)y, (float)measure.Width, h),
stringFormat);
@@ -304,13 +312,13 @@ public void DrawImage (object backend, object img, double x, double y, double wi
public void ResetTransform (object backend)
{
var c = (DrawingContext)backend;
- c.Graphics.ResetTransform();
+ c.Graphics.ResetTransform ();
}
public void Rotate (object backend, double angle)
{
var c = (DrawingContext)backend;
- c.Graphics.RotateTransform((float)angle);
+ c.Graphics.RotateTransform ((float)angle);
}
public void Scale (object backend, double scaleX, double scaleY)
@@ -329,7 +337,7 @@ public void TransformPoint (object backend, ref double x, ref double y)
{
Matrix m = ((DrawingContext)backend).Graphics.Transform;
PointF p = new PointF ((float)x, (float)y);
- PointF[] pts = new PointF[] {p};
+ PointF[] pts = new PointF[] { p };
m.TransformPoints (pts);
x = pts[0].X;
y = pts[0].Y;
@@ -377,6 +385,7 @@ public void TransformDistances (object backend, Distance[] vectors)
public void Dispose (object backend)
{
+ ((DrawingContext)backend).Dispose();
}
internal static void DrawImageCore (Graphics g, Bitmap bmp, float x, float y, float width, float height, float alpha)
View
24 Xwt.WPF/Xwt.WPFBackend/DrawingContext.cs
@@ -32,6 +32,7 @@
namespace Xwt.WPFBackend
{
internal class DrawingContext
+ : IDisposable
{
internal DrawingContext (Graphics graphics)
{
@@ -58,6 +59,23 @@ internal DrawingContext (DrawingContext context)
CurrentY = context.CurrentY;
}
+ public void Dispose()
+ {
+ Font.Dispose();
+ Pen.Dispose();
+ Brush.Dispose();
+ Path.Dispose();
+
+ if (Transform != null)
+ Transform.Dispose();
+
+ if (this.contexts != null) {
+ foreach (var drawingContext in this.contexts) {
+ drawingContext.Dispose ();
+ }
+ }
+ }
+
internal readonly Graphics Graphics;
internal Font Font = new Font (FontFamily.GenericSansSerif, 12);
@@ -70,12 +88,6 @@ internal DrawingContext (DrawingContext context)
internal GraphicsPath Path = new GraphicsPath();
- internal void SetColor (Color color)
- {
- Pen.Color = color;
- Brush = new SolidBrush (color);
- }
-
internal void Save()
{
if (this.contexts == null)
Please sign in to comment.
Something went wrong with that request. Please try again.