Dziwnie rysowane GraphicsPath z Graphics.FillPath
Napisałem kod, który tworzy zaokrąglony prostokątGraphicsPath
, w oparciu o niestandardową strukturę,BorderRadius
(co pozwala mi zdefiniować górny lewy, prawy górny, lewy dolny i prawy dolny promień prostokąta) oraz początkowyRectangle
samo:
public static GraphicsPath CreateRoundRectanglePath(BorderRadius radius, Rectangle rectangle)
{
GraphicsPath result = new GraphicsPath();
if (radius.TopLeft > 0)
{
result.AddArc(rectangle.X, rectangle.Y, radius.TopLeft, radius.TopLeft, 180, 90);
}
else
{
result.AddLine(new System.Drawing.Point(rectangle.X, rectangle.Y), new System.Drawing.Point(rectangle.X, rectangle.Y));
}
if (radius.TopRight > 0)
{
result.AddArc(rectangle.X + rectangle.Width - radius.TopRight, rectangle.Y, radius.TopRight, radius.TopRight, 270, 90);
}
else
{
result.AddLine(new System.Drawing.Point(rectangle.X + rectangle.Width, rectangle.Y), new System.Drawing.Point(rectangle.X + rectangle.Width, rectangle.Y));
}
if (radius.BottomRight > 0)
{
result.AddArc(rectangle.X + rectangle.Width - radius.BottomRight, rectangle.Y + rectangle.Height - radius.BottomRight, radius.BottomRight, radius.BottomRight, 0, 90);
}
else
{
result.AddLine(new System.Drawing.Point(rectangle.X + rectangle.Width, rectangle.Y + rectangle.Height), new System.Drawing.Point(rectangle.X + rectangle.Width, rectangle.Y + rectangle.Height));
}
if (radius.BottomLeft > 0)
{
result.AddArc(rectangle.X, rectangle.Y + rectangle.Height - radius.BottomLeft, radius.BottomLeft, radius.BottomLeft, 90, 90);
}
else
{
result.AddLine(new System.Drawing.Point(rectangle.X, rectangle.Y + rectangle.Height), new System.Drawing.Point(rectangle.X, rectangle.Y + rectangle.Height));
}
return result;
}
Teraz, jeśli użyję tego wraz z FillPath i DrawPath, zauważę dziwne wyniki:
GraphicsPath path = CreateRoundRectanglePath(new BorderRadius(8), new Rectangle(10, 10, 100, 100));
e.Graphics.DrawPath(new Pen(Color.Black, 1), path);
e.Graphics.FillPath(new SolidBrush(Color.Black), path);
Powiększałem każdy wynikRectangle
(po prawej stronie), aby wyraźnie zobaczyć problem:
Chciałbym wiedzieć: Dlaczego wszystkie łuki na narysowanym prostokącie są równe, a wszystkie łuki na wypełnionym prostokącie są nieparzyste?
Co więcej, czy można to naprawić, aby wypełniony prostokąt rysował poprawnie?
EDYCJA: Czy możliwe jest wypełnienie wnętrza GraphicsPath bez użycia FillPath?
EDYCJA: Jak na komentarze .... oto przykład struktury BorderRadius
public struct BorderRadius
{
public Int32 TopLeft { get; set; }
public Int32 TopRight { get; set; }
public Int32 BottomLeft { get; set; }
public Int32 BottomRight { get; set; }
public BorderRadius(int all) : this()
{
this.TopLeft = this.TopRight = this.BottomLeft = this.BottomRight = all;
}
}