C# WPF中六边形的填充颜色
我在用这段代码填充六边形时遇到了问题,当这段代码运行时,它只绘制了六边形的“白色”轮廓,我想用颜色填充六边形,但它不起作用 我搜索了很多,尝试了很多东西,比如C# WPF中六边形的填充颜色,c#,wpf,shapes,C#,Wpf,Shapes,我在用这段代码填充六边形时遇到了问题,当这段代码运行时,它只绘制了六边形的“白色”轮廓,我想用颜色填充六边形,但它不起作用 我搜索了很多,尝试了很多东西,比如drawingContext.Drawing(),drawingBrush,等等 我在这段代码中遗漏了什么吗?代码如下: public void DrawHexagon(DrawingContext drawingContext) { GeometryGroup hexaKey = new GeometryGroup();
drawingContext.Drawing()
,drawingBrush
,等等
我在这段代码中遗漏了什么吗?代码如下:
public void DrawHexagon(DrawingContext drawingContext)
{
GeometryGroup hexaKey = new GeometryGroup();
//making lines for hexagon
hexaKey.Children.Add(
new LineGeometry(new Point(X1, Y1), new Point(X2, Y2)));
hexaKey.Children.Add(
new LineGeometry(new Point(X2, Y2), new Point(X3, Y3)));
hexaKey.Children.Add(
new LineGeometry(new Point(X3, Y3), new Point(X4, Y4)));
hexaKey.Children.Add(
new LineGeometry(new Point(X4, Y4), new Point(X5, Y5)));
hexaKey.Children.Add(
new LineGeometry(new Point(X5, Y5), new Point(X6, Y6)));
hexaKey.Children.Add(
new LineGeometry(new Point(X6, Y6), new Point(X1, Y1)));
//
// Create a GeometryDrawing.
//
GeometryDrawing hexaKeyDrawing = new GeometryDrawing();
hexaKeyDrawing.Geometry = hexaKey;
// Paint the drawing with a gradient.
hexaKeyDrawing.Brush =new SolidColorBrush(Colors.Red);
// Outline the drawing with a solid color.
hexaKeyDrawing.Pen = new Pen(Brushes.White, 2);
drawingContext.DrawGeometry(hexaKeyDrawing.Brush, hexaKeyDrawing.Pen, hexaKeyDrawing.Geometry);
}
线条几何体无法填充。。。它们只是台词。你需要一条路。线几何体无法填充。。。它们只是台词。你需要一条路。示例,如何填充六边形:示例,如何填充六边形:在您的示例中,在
几何图形组中有许多线几何图形实例
首先,我要链接提醒您注意,您实际上并没有使用GeometryDrawing
。在您的示例中,您仅将其用作GemetryGroup
的占位符
忽略这一点,问题在于,LineGeometry
实例不用于绘制形状,GeometryGroup
无法意识到6条线段一起构成闭合形状
但是不要绝望,因为有一种方法可以实现你想要的:PathGeometry
。该几何体基本上定义了可填充区域的轮廓!此轮廓可由起点和一系列路径段定义
private Point GetExtremity(Point center, double radius, double orientation)
{
return new Point(
center.X + Math.Cos(orientation) * radius,
center.Y + Math.Sin(orientation) * radius
);
}
public void DrawUniformShape(DrawingContext context, Brush brush, Pen pen, Point center, double radius, int sides, double orientationRadians)
{
context.DrawGeometry(
brush,
pen,
new PathGeometry(
Enumerable.Repeat(
new PathFigure(
GetExtremity(center, radius, orientationRadians),
from vertex in Enumerable.Range(1, sides - 1)
let angle = orientationRadians + vertex * 2 * Math.PI / sides
select new LineSegment(GetExtremity(center, radius, angle), true),
true
),
1
)
)
);
}
public void DrawBarnColouredHexagon(DrawingContext context, Point center, double radius, double orientation)
{
DrawUniformShape(
context,
Brushes.Red,
new Pen(Brushes.White, 2),
center,
radius,
6,
0
);
}
在您的示例中,GeometryGroup中有许多LineGeometry
实例,GeometryDrawing
首先,我要链接提醒您注意,您实际上并没有使用GeometryDrawing
。在您的示例中,您仅将其用作GemetryGroup
的占位符
忽略这一点,问题在于,LineGeometry
实例不用于绘制形状,GeometryGroup
无法意识到6条线段一起构成闭合形状
但是不要绝望,因为有一种方法可以实现你想要的:PathGeometry
。该几何体基本上定义了可填充区域的轮廓!此轮廓可由起点和一系列路径段定义
private Point GetExtremity(Point center, double radius, double orientation)
{
return new Point(
center.X + Math.Cos(orientation) * radius,
center.Y + Math.Sin(orientation) * radius
);
}
public void DrawUniformShape(DrawingContext context, Brush brush, Pen pen, Point center, double radius, int sides, double orientationRadians)
{
context.DrawGeometry(
brush,
pen,
new PathGeometry(
Enumerable.Repeat(
new PathFigure(
GetExtremity(center, radius, orientationRadians),
from vertex in Enumerable.Range(1, sides - 1)
let angle = orientationRadians + vertex * 2 * Math.PI / sides
select new LineSegment(GetExtremity(center, radius, angle), true),
true
),
1
)
)
);
}
public void DrawBarnColouredHexagon(DrawingContext context, Point center, double radius, double orientation)
{
DrawUniformShape(
context,
Brushes.Red,
new Pen(Brushes.White, 2),
center,
radius,
6,
0
);
}
我也尝试过这种方法,但是这个呈现路径的示例使用了UI框架元素。我需要使用DrawingContext实现它。我尝试通过添加pathGeometry来实现pathGeometry;将填充规则设为非零,然后在drawingContext中使用此几何体,但结果仍然相同,只有轮廓存在。我想填充整个六边形。我也尝试过这种方法,但这个呈现路径的示例使用UI框架元素。我需要使用DrawingContext实现它。我尝试通过添加pathGeometry来实现pathGeometry;将填充规则设为非零,然后在drawingContext中使用此几何体,但结果仍然相同,只有轮廓存在。我想填充整个六边形。可能是重复的是的,它工作,谢谢你的帮助可能是重复的是的,它工作,谢谢你的帮助