Skip to content

PathGradientBrush fails to render a complex polygon #117

Description

@chris-steema

SixLabors.ImageSharp v1.0.2
SixLabors.ImageSharp.Drawing v1.0.0-beta11
Running in Visual Studio 2019 v.16.8.3 on Windows 10 v.2004 in .NET 5.0.1-servicing.20575.16

Using the SixLabors.ImageSharp.Drawing.Processing.FillPathExtensions's Fill method, I can render a complex polygon using a SolidBrush, but using a PathGradientBrush the program doesn't finish executing and 'hangs' with high memory usage.

Code to reproduce:

        private static void Main(string[] args)
        {
            static void GradientPolygon()
            {
                static void DoComplexPolygon(IImageProcessingContext g, RectangleF rect)
                {
                    static PointF PointFromCircle(RectangleF rectBounds, double degrees)
                    {
                        static void RectCenter(RectangleF r, out float x, out float y)
                        {
                            x = (r.Left + r.Right) / 2.0f;
                            y = (r.Top + r.Bottom) / 2.0f;
                        }

                        RectCenter(rectBounds, out var xCenter, out var yCenter);
                        var radians = (Math.PI * degrees) / 180.0f;
                        var radiusH = rectBounds.Width / 2.0f;
                        var radiusV = rectBounds.Height / 2.0f;

                        var tmpX = xCenter + radiusH * (float)Math.Cos(radians);
                        var tmpY = yCenter - radiusV * (float)Math.Sin(radians);

                        return new PointF(tmpX, tmpY);
                    }

                    var iPoints = new List<PointF>();

                    for (double i = 0; i <= 360; i += 0.1)
                    {
                        iPoints.Add(PointFromCircle(rect, i));
                    }
                    var widthReduction = 50;

                    rect.Inflate(-widthReduction, -widthReduction);

                    for (double i = 360; i >= 0; i -= 0.1)
                    {
                        iPoints.Add(PointFromCircle(rect, i));
                    }

                    var path = new SixLabors.ImageSharp.Drawing.Polygon(new LinearLineSegment(iPoints.ToArray()));

                    //g.Fill(new SolidBrush(Color.Red), path); //this works
                    g.Fill(new PathGradientBrush(iPoints.ToArray(), new Color[] { Color.Red, Color.White, Color.Blue }), path); //this does not complete
                }

                var bitmap = new Image<Rgba32>(800, 600);

                bitmap.Mutate(x =>
                {
                    x.Clear(Color.White);
                    DoComplexPolygon(x, new RectangleF(137, 50, 526, 526));
                });

                var path = $@"{AppDomain.CurrentDomain.BaseDirectory}/Polygon{DateTime.Now.Ticks}.png";

                bitmap.SaveAsPng(path);

                new Process
                {
                    StartInfo = new ProcessStartInfo(path)
                    {
                        UseShellExecute = true
                    }
                }.Start();
            }

            GradientPolygon();
        }
    }

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions