Я написал код, который создает округленный прямоугольник GraphicsPath
, основанный на пользовательской структуре, BorderRadius
(что позволяет мне определять левый верхний правый, правый нижний и нижний правый радиус прямоугольник), а сам начальная Rectangle
:Странно рисованный GraphicsPath с Graphics.FillPath
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;
}
Теперь, если я использую это наряду с FillPath и DrawPath, я заметил некоторые странные результаты:
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);
Я увеличенный в каждый полученного Rectangle
(правая сторона), так что вы можете ясно видеть, проблему:
То, что я хотел бы знать: Почему все из дуг на нарисованном прямоугольнике равны, и все дуги на заполненный прямоугольник, нечетный?
Лучше все еще, можно ли зафиксировать его, чтобы правильно заполнить прямоугольник?
EDIT: Возможно ли заполнить внутреннюю часть GraphicsPath без использования FillPath?
EDIT: В соответствии с комментариями .... вот пример 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;
}
}
Можете ли вы разместить свой BorderRadius(), чтобы мы могли играть с примером? –
@Idle_Mind, см. Обновление – series0ne