Repository navigation
Description
Prerequisites
- I have written a descriptive issue title
- I have verified that I am running the latest version of ImageSharp.Drawing
- I have verified if the problem exist in both
DEBUGandRELEASEmode - I have searched open and closed issues to ensure it has not already been reported
ImageSharp.Drawing version
main at commit a4e94b8
Other ImageSharp packages and versions
SixLabors.ImageSharp 5.0.0-alpha.0.38, SixLabors.Fonts 3.2.1, SixLabors.PolygonClipper 2.0.0-alpha.0.2
Environment (Operating system, version and so on)
macOS 15.8.3 (arm64)
.NET Framework version
.NET 10 (net10.0)
Description
SplitPathExtensions.GenerateDashes returns the original, undashed path when its estimate of dash pieces for one subpath exceeds 10,000 (dashes plus gaps), and also when the whole pattern is shorter than 1e-6 units. Draw(Pen, IPath) and Fill(Pen, IPath) then draw a solid stroke. A dotted line with dash 1 and gap 1 at width 1 is drawn dotted up to a length of 10,000 pixels and solid from 10,002 pixels. Nothing tells the caller that the pattern was dropped.
Explanation
SplitPathExtensions.GenerateDashes (SplitPathExtensions.cs) has two fallbacks:
- Lines 80 to 84: if the sum of the pattern lengths (times the width) is at most
eps = 1e-6, it returnspath. - Lines 136 to 145: for each flattened subpath it estimates
(totalLength / patternLength) * pattern.Length, and if that is aboveMaxPatternSegments(10,000, line 17), it returnspathfor the whole input, including subpaths that were already split.
The comment at line 14 says this is a safety limit "to avoid runaway segmentation from very short patterns applied to very long paths". OutlinePathExtensions.GenerateOutline (line 161) and DrawingCanvasBatcher<TPixel> (line 768) stroke the returned path as a solid line.
The limit is reached by ordinary drawings: a dotted grid line or a dotted route across a large image. The result is a different picture, not only a faster one.
Suggested fix
Do not fall back to a solid stroke for a long path. Options, from simplest:
- Remove the per-subpath estimate, or raise it far above real drawings, and bound the work by the area that can be drawn instead: dashes outside the clip or target bounds do not need to be emitted. The canvas knows the target bounds;
GenerateDashescould take an optional interest rectangle and only emit dashes whose segment intersects it (the walk along the path is cheap; the cost is in the emitted subpaths and their strokes). - If a hard limit is kept, apply it to the total output of the call, and document it on
GenerateDashes,PatternPen, andDraw(Pen, IPath), so that callers know when the result is not dashed.
For the very short pattern (fallback 1), a solid stroke is one possible approximation, but it draws twice the ink of a pattern with equal dash and gap. Consider drawing the stroke with the average coverage of the pattern (dash length divided by pattern length) when the pattern is below the pixel size, or document the current choice.
The SVG work does not change this code.
Notes
- Found in the SVG work:
pathLength="1e30"scales the dash pattern of a stroke down to about 1e-28 units, and the core then draws a solid stroke. That SVG case is the very short pattern of fallback 1. A browser was reported to draw nothing for it; that was not run here. - The limit counts dashes and gaps, so it is reached at 5,000 dashes. The pattern
[1, 1]at width 1 reaches it at a length of 10,000 units. - Not measured: the time and memory of
GenerateDashesand of the stroke for 5,000, 50,000, and 500,000 dashes. A fix should measure them to choose a bound.
Steps to Reproduce
Project file (Repro.csproj). It references a Release build of main; the path is a placeholder.
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net10.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>
<ItemGroup>
<Reference Include="SixLabors.ImageSharp.Drawing" HintPath="path/to/SixLabors.ImageSharp.Drawing.dll" />
<PackageReference Include="SixLabors.ImageSharp" Version="5.0.0-alpha.0.38" />
<PackageReference Include="SixLabors.Fonts" Version="3.2.1" />
<PackageReference Include="SixLabors.PolygonClipper" Version="2.0.0-alpha.0.2" />
</ItemGroup>
</Project>Program.cs:
using SixLabors.ImageSharp;
using SixLabors.ImageSharp.Drawing;
using SixLabors.ImageSharp.Drawing.Processing;
using SixLabors.ImageSharp.PixelFormats;
using SixLabors.ImageSharp.Processing;
using Path = SixLabors.ImageSharp.Drawing.Path;
// A dotted horizontal line (dash 1, gap 1, width 1) through the centre of pixel row 100.
// Only the first 200 pixels are inside the image; the rest of the line is outside.
float[] dotted = [1F, 1F];
foreach (float length in new[] { 9_000F, 10_000F, 10_002F, 12_000F })
{
IPath line = new Path(new LinearLineSegment(new PointF(0, 100.5F), new PointF(length, 100.5F)));
int subpaths = line.GenerateDashes(1, dotted).Flatten().Count();
using Image<Rgba32> image = new(200, 200, Color.White.ToPixel<Rgba32>());
image.Mutate(c => c.Paint(canvas => canvas.Draw(new PatternPen(new PenOptions(Color.Black, 1, dotted)), line)));
int white = Enumerable.Range(0, 200).Count(x => image[x, 100].R == 255);
Console.WriteLine($"line length {length,6}: GenerateDashes gives {subpaths,5} subpaths; row 100 has {white,3} white pixels of 200");
}
// A very short pattern: dash and gap of 1e-8 times the width.
IPath shortLine = new Path(new LinearLineSegment(new PointF(20, 100), new PointF(180, 100)));
float[] tiny = [1e-8F, 1e-8F];
Console.WriteLine($"pattern 1e-8: GenerateDashes returns the input path: {ReferenceEquals(shortLine.GenerateDashes(10, tiny), shortLine)}");Run with dotnet run -c Release. A Debug build gives the same output.
Observed output:
line length 9000: GenerateDashes gives 4500 subpaths; row 100 has 100 white pixels of 200
line length 10000: GenerateDashes gives 5000 subpaths; row 100 has 100 white pixels of 200
line length 10002: GenerateDashes gives 1 subpaths; row 100 has 0 white pixels of 200
line length 12000: GenerateDashes gives 1 subpaths; row 100 has 0 white pixels of 200
pattern 1e-8: GenerateDashes returns the input path: True
Expected: the visible part of the line looks the same for every length: 100 white pixels of 200 in row 100. The length of the line outside the image should not change what is drawn inside it.
Images
Rows 90 to 110 of the output, enlarged 4 times. Top: the line 10,000 units long, drawn dotted. Bottom: the line 10,002 units long, drawn solid. Only the first 200 units of both lines are inside the image.

Activity