From 203e84a4218827327204633913eec451eb0ac6a7 Mon Sep 17 00:00:00 2001 From: izakusilov Date: Sun, 25 Oct 2026 15:42:04 +0000 Subject: [PATCH 1/7] Update Avalonia to 12.1.4 --- Source/Directory.Build.props | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Source/Directory.Build.props b/Source/Directory.Build.props index 1f55fde..69caaa0 100644 --- a/Source/Directory.Build.props +++ b/Source/Directory.Build.props @@ -1,6 +1,6 @@ - 12.0.2 + 12.1.4 2.2.0 \ No newline at end of file From c4b9be0a277accd4629f95f534f28cb4266383fc Mon Sep 17 00:00:00 2001 From: izakusilov Date: Sat, 24 Oct 2026 13:47:52 +0000 Subject: [PATCH 2/7] Skip shapes with non-finite coordinates --- .../OxyPlot.Avalonia/CanvasRenderContext.cs | 103 ++++++++++++++++++ 1 file changed, 103 insertions(+) diff --git a/Source/OxyPlot.Avalonia/CanvasRenderContext.cs b/Source/OxyPlot.Avalonia/CanvasRenderContext.cs index 17762c3..457b923 100644 --- a/Source/OxyPlot.Avalonia/CanvasRenderContext.cs +++ b/Source/OxyPlot.Avalonia/CanvasRenderContext.cs @@ -102,6 +102,11 @@ public override void DrawEllipse(OxyRect rect, double thickness, EdgeRenderingMode edgeRenderingMode) { + if (!IsFinite(rect)) + { + return; + } + var path = CreateAndAdd(); SetStroke(path, stroke, thickness, edgeRenderingMode); if (!fill.IsUndefined()) @@ -136,6 +141,11 @@ private void DrawEllipsesByStreamGeometry( foreach (var rect in rects) { + if (!IsFinite(rect)) + { + continue; + } + if (path == null) { path = CreateAndAdd(); @@ -220,6 +230,12 @@ public override void DrawLine( double[] dashArray, LineJoin lineJoin) { + points = FiniteOnly(points); + if (points.Count == 0) + { + return; + } + if (thickness < BalancedLineDrawingThicknessLimit) { DrawLineBalanced(points, stroke, thickness, edgeRenderingMode, dashArray, lineJoin); @@ -260,6 +276,11 @@ private void DrawLineSegmentsByStreamGeometry( for (int i = 0; i + 1 < points.Count; i += 2) { + if (!IsFinite(points[i]) || !IsFinite(points[i + 1])) + { + continue; + } + if (streamGeometry == null) { streamGeometry = new StreamGeometry(); @@ -325,6 +346,11 @@ public override void DrawLineSegments( for (int i = 0; i + 1 < points.Count; i += 2) { + if (!IsFinite(points[i]) || !IsFinite(points[i + 1])) + { + continue; + } + if (path == null) { path = CreateAndAdd(); @@ -365,6 +391,11 @@ public override void DrawPolygon( double[] dashArray, LineJoin lineJoin) { + if (!IsFinite(points)) + { + return; + } + var e = CreateAndAdd(); SetStroke(e, stroke, thickness, edgeRenderingMode, lineJoin, dashArray, 0); @@ -410,6 +441,11 @@ private void DrawPolygonsByStreamGeometry( foreach (var polygon in polygons) { + if (!IsFinite(polygon)) + { + continue; + } + if (path == null) { path = CreateAndAdd(); @@ -498,6 +534,11 @@ public override void DrawPolygons( foreach (var polygon in polygons) { + if (!IsFinite(polygon)) + { + continue; + } + if (path == null) { path = CreateAndAdd(); @@ -563,6 +604,11 @@ public override void DrawRectangle(OxyRect rect, double thickness, EdgeRenderingMode edgeRenderingMode) { + if (!IsFinite(rect)) + { + return; + } + var path = CreateAndAdd(); SetStroke(path, stroke, thickness, edgeRenderingMode); if (!fill.IsUndefined()) @@ -615,6 +661,11 @@ public override void DrawText( VerticalAlignment valign, OxySize? maxSize) { + if (!IsFinite(p)) + { + return; + } + var tb = CreateAndAdd(); tb.Text = text; tb.Foreground = GetCachedBrush(fill); @@ -1105,6 +1156,58 @@ private static Rect ToRect(OxyRect r) return new Rect(r.Left, r.Top, r.Width, r.Height); } + // A shape with NaN or infinite coordinates breaks the whole Canvas layout pass + // (InvalidOperationException from Measure), not just itself. The core produces such + // coordinates for annotations that are not fully configured, so these shapes are not drawn. + + /// + /// Checks that all coordinates of the rectangle are finite. + /// + /// The rectangle. + /// true if the shape can be drawn. + private static bool IsFinite(OxyRect r) + { + return double.IsFinite(r.Left) && double.IsFinite(r.Top) && double.IsFinite(r.Width) && double.IsFinite(r.Height); + } + + /// + /// Checks that both coordinates of the point are finite. + /// + /// The point. + /// true if the point can be drawn. + private static bool IsFinite(ScreenPoint p) + { + return double.IsFinite(p.X) && double.IsFinite(p.Y); + } + + /// + /// Checks that the coordinates of all points are finite. + /// + /// The points. + /// true if the shape can be drawn. + private static bool IsFinite(IList points) + { + for (var i = 0; i < points.Count; i++) + { + if (!IsFinite(points[i])) + { + return false; + } + } + + return true; + } + + /// + /// Removes points with non-finite coordinates from a line. If there are none, returns the same list. + /// + /// The points of the line. + /// The points with finite coordinates. + private static IList FiniteOnly(IList points) + { + return IsFinite(points) ? points : points.Where(IsFinite).ToList(); + } + /// /// Converts an to a pixel aligned . /// From 2a4fc04210a43dc96c16d3ca15d18f8a2c575c44 Mon Sep 17 00:00:00 2001 From: izakusilov Date: Sat, 24 Oct 2026 13:47:52 +0000 Subject: [PATCH 3/7] Measure text without size limit --- Source/OxyPlot.Avalonia/CanvasRenderContext.cs | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/Source/OxyPlot.Avalonia/CanvasRenderContext.cs b/Source/OxyPlot.Avalonia/CanvasRenderContext.cs index 457b923..03c99cd 100644 --- a/Source/OxyPlot.Avalonia/CanvasRenderContext.cs +++ b/Source/OxyPlot.Avalonia/CanvasRenderContext.cs @@ -689,7 +689,9 @@ public override void DrawText( if (maxSize != null || halign != HorizontalAlignment.Left || valign != VerticalAlignment.Top) { - tb.Measure(new Size(1000, 1000)); + // Measure without a limit: the hard-coded 1000x1000 clipped long labels. + // The required truncation is done by the block below according to maxSize. + tb.Measure(Size.Infinity); var size = tb.DesiredSize; if (maxSize != null) { @@ -781,7 +783,8 @@ public override OxySize MeasureText(string text, tb.FontWeight = GetFontWeight(fontWeight); } - tb.Measure(new Size(1000, 1000)); + // Measure without a limit: the hard-coded 1000x1000 understated the size of long labels. + tb.Measure(Size.Infinity); return new OxySize(tb.DesiredSize.Width, tb.DesiredSize.Height); } From c5c4351e38a7c0924999fcf35535efe7d74f5647 Mon Sep 17 00:00:00 2001 From: izakusilov Date: Sat, 24 Oct 2026 13:48:03 +0000 Subject: [PATCH 4/7] Fix image cropping in CanvasRenderContext --- .../OxyPlot.Avalonia/CanvasRenderContext.cs | 32 ++++++++++++------- 1 file changed, 21 insertions(+), 11 deletions(-) diff --git a/Source/OxyPlot.Avalonia/CanvasRenderContext.cs b/Source/OxyPlot.Avalonia/CanvasRenderContext.cs index 03c99cd..c421552 100644 --- a/Source/OxyPlot.Avalonia/CanvasRenderContext.cs +++ b/Source/OxyPlot.Avalonia/CanvasRenderContext.cs @@ -825,25 +825,33 @@ public override void DrawImage( double opacity, bool interpolate) { - if (destWidth <= 0 || destHeight <= 0 || srcWidth <= 0 || srcHeight <= 0) + if (!(destWidth > 0) || !(destHeight > 0) || !(srcWidth > 0) || !(srcHeight > 0) + || !double.IsFinite(destX) || !double.IsFinite(destY) + || !double.IsFinite(destWidth) || !double.IsFinite(destHeight)) { return; } - var image = CreateAndAdd(destX, destY); - var bitmapChain = GetImageSource(source); - - // ReSharper disable CompareOfFloatsByEqualityOperator - if (srcX == 0 && srcY == 0 && srcWidth == bitmapChain.PixelSize.Width && srcHeight == bitmapChain.PixelSize.Height) - // ReSharper restore CompareOfFloatsByEqualityOperator + var bitmap = GetImageSource(source); + if (bitmap == null) { - // do not crop + return; } - else + + // The part of the source is cropped by whole pixels within the bitmap. Clip is not suitable here: + // it is specified in the coordinates of the element, not of the source, and overrides the clipping to the plot area. + var left = Math.Max(0, (int)Math.Floor(srcX)); + var top = Math.Max(0, (int)Math.Floor(srcY)); + var right = Math.Min(bitmap.PixelSize.Width, (int)Math.Ceiling(srcX + srcWidth)); + var bottom = Math.Min(bitmap.PixelSize.Height, (int)Math.Ceiling(srcY + srcHeight)); + if (right <= left || bottom <= top) { - image.Clip = new RectangleGeometry(new Rect(srcX, srcY, srcWidth, srcHeight)); + return; } + var image = CreateAndAdd(destX, destY); + var isWholeBitmap = left == 0 && top == 0 && right == bitmap.PixelSize.Width && bottom == bitmap.PixelSize.Height; + image.Opacity = opacity; image.Width = destWidth; image.Height = destHeight; @@ -854,7 +862,9 @@ public override void DrawImage( Canvas.SetTop(image, destY); //// alternative: image.RenderTransform = new TranslateTransform(destX, destY); - image.Source = bitmapChain; + image.Source = isWholeBitmap + ? bitmap + : new CroppedBitmap(bitmap, new PixelRect(left, top, right - left, bottom - top)); if (interpolate) { From 07fecbba4b48690495b28beb9a32f6266093d7dd Mon Sep 17 00:00:00 2001 From: izakusilov Date: Sat, 24 Oct 2026 13:48:03 +0000 Subject: [PATCH 5/7] Compute anti-aliasing mode once per line --- Source/OxyPlot.Avalonia/CanvasRenderContext.cs | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/Source/OxyPlot.Avalonia/CanvasRenderContext.cs b/Source/OxyPlot.Avalonia/CanvasRenderContext.cs index c421552..d9ac737 100644 --- a/Source/OxyPlot.Avalonia/CanvasRenderContext.cs +++ b/Source/OxyPlot.Avalonia/CanvasRenderContext.cs @@ -1088,9 +1088,13 @@ private void DrawLineBalanced(IList points, OxyColor stroke, double double lineLength = 0; var dashPatternLength = (dashArray != null) ? dashArray.Sum() : 0; var last = new Point(); + + // The anti-aliasing mode does not depend on the point index, while the check walks the whole list, + // so it is computed once before the loop. Inside the loop it gave quadratic complexity. + var aliased = !this.ShouldUseAntiAliasingForLine(edgeRenderingMode, points); + for (int i = 0; i < n; i++) { - bool aliased = !this.ShouldUseAntiAliasingForLine(edgeRenderingMode, points); var p = aliased ? ToPixelAlignedPoint(points[i]) : ToPoint(points[i]); pc.Add(p); From 86c4886f80ed54d535139837d85cb67b7ccb7b89 Mon Sep 17 00:00:00 2001 From: izakusilov Date: Sat, 24 Oct 2026 13:49:00 +0000 Subject: [PATCH 6/7] Dispose bitmap after exporting to PNG --- Source/OxyPlot.Avalonia/PngExporter.cs | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/Source/OxyPlot.Avalonia/PngExporter.cs b/Source/OxyPlot.Avalonia/PngExporter.cs index e8696a0..1e0b4d4 100644 --- a/Source/OxyPlot.Avalonia/PngExporter.cs +++ b/Source/OxyPlot.Avalonia/PngExporter.cs @@ -75,7 +75,7 @@ public static void Export(IPlotModel model, Stream stream, int width, int height /// The height. /// The background. /// The resolution (dpi). - /// A bitmap. + /// A bitmap. The caller must dispose it. public static Bitmap ExportToBitmap( IPlotModel model, int width, @@ -94,8 +94,9 @@ public static Bitmap ExportToBitmap( /// The output stream. public void Export(IPlotModel model, Stream stream) { - var bmp = ExportToBitmap(model); - bmp.Save(stream); + // The bitmap holds a native surface, so it is disposed right after writing. + using var bmp = ExportToBitmap(model); + bmp.Save(stream, PngBitmapEncoderOptions.Default); } /// From dde393848283a78659ea16a96c2afd3adf6ad1e4 Mon Sep 17 00:00:00 2001 From: izakusilov Date: Sat, 24 Oct 2026 13:49:00 +0000 Subject: [PATCH 7/7] Validate model and size in SaveBitmap and ToBitmap --- Source/OxyPlot.Avalonia/PlotBase.Export.cs | 65 ++++++++++++++++++++-- Source/OxyPlot.Avalonia/PngExporter.cs | 7 ++- 2 files changed, 67 insertions(+), 5 deletions(-) diff --git a/Source/OxyPlot.Avalonia/PlotBase.Export.cs b/Source/OxyPlot.Avalonia/PlotBase.Export.cs index a41f100..6859ec7 100644 --- a/Source/OxyPlot.Avalonia/PlotBase.Export.cs +++ b/Source/OxyPlot.Avalonia/PlotBase.Export.cs @@ -10,6 +10,7 @@ namespace OxyPlot.Avalonia { using global::Avalonia.Media.Imaging; + using System; using System.IO; /// @@ -23,7 +24,7 @@ public partial class PlotBase /// Stream to which to write the bitmap. public void SaveBitmap(Stream stream) { - SaveBitmap(stream, -1, -1, ActualModel.Background); + SaveBitmap(stream, -1, -1, GetModelForExport().Background); } /// @@ -35,6 +36,8 @@ public void SaveBitmap(Stream stream) /// The background. public void SaveBitmap(Stream stream, int width, int height, OxyColor background) { + var model = GetModelForExport(); + if (width <= 0) { width = (int)Bounds.Width; @@ -45,12 +48,25 @@ public void SaveBitmap(Stream stream, int width, int height, OxyColor background height = (int)Bounds.Height; } + CheckExportSize(width, height); + if (!background.IsVisible()) { background = Background.ToOxyColor(); } - PngExporter.Export(ActualModel, stream, width, height, background); + try + { + lock (model.SyncRoot) + { + PngExporter.Export(model, stream, width, height, background); + } + } + finally + { + // The export recalculated the live model for its own size, so it is restored to the size of the control + InvalidatePlot(false); + } } /// @@ -59,8 +75,49 @@ public void SaveBitmap(Stream stream, int width, int height, OxyColor background /// A bitmap. public Bitmap ToBitmap() { - var background = ActualModel.Background.IsVisible() ? ActualModel.Background : Background.ToOxyColor(); - return PngExporter.ExportToBitmap(ActualModel, (int)Bounds.Width, (int)Bounds.Height, background); + var model = GetModelForExport(); + var width = (int)Bounds.Width; + var height = (int)Bounds.Height; + CheckExportSize(width, height); + + var background = model.Background.IsVisible() ? model.Background : Background.ToOxyColor(); + try + { + lock (model.SyncRoot) + { + return PngExporter.ExportToBitmap(model, width, height, background); + } + } + finally + { + // The export recalculated the live model for its own size, so it is restored to the size of the control + InvalidatePlot(false); + } + } + + /// + /// Gets the model for export. + /// + /// The current model. + /// There is no model. + private PlotModel GetModelForExport() + { + return ActualModel ?? throw new InvalidOperationException("Cannot export a plot without a model: the Model property is not set."); + } + + /// + /// Validates the bitmap size for export. + /// + /// The width. + /// The height. + /// The size is less than one pixel. + private static void CheckExportSize(int width, int height) + { + if (width < 1 || height < 1) + { + throw new InvalidOperationException( + "The control has not been laid out yet or is hidden, so its size is zero. Specify the width and height explicitly in SaveBitmap."); + } } } } diff --git a/Source/OxyPlot.Avalonia/PngExporter.cs b/Source/OxyPlot.Avalonia/PngExporter.cs index 1e0b4d4..d991261 100644 --- a/Source/OxyPlot.Avalonia/PngExporter.cs +++ b/Source/OxyPlot.Avalonia/PngExporter.cs @@ -103,7 +103,12 @@ public void Export(IPlotModel model, Stream stream) /// Exports the specified plot model to a bitmap. /// /// The model to export. - /// A bitmap. + /// A bitmap. The caller must dispose it. + /// + /// The model is updated and rendered for the export size. If it is shown in a control, + /// it must be redrawn after the export (InvalidatePlot), otherwise the tracker and zooming + /// work with the geometry of the export. The SaveBitmap and ToBitmap methods of the control do this themselves. + /// public Bitmap ExportToBitmap(IPlotModel model) { var scale = 96d / Resolution;