gpt4 book ai didi

c# - 在 DrawingContext 中绘制一个实心三角形

转载 作者:可可西里 更新时间:2023-11-01 08:55:25 26 4
gpt4 key购买 nike

这是此 question 的跟进.

问题:

我正在尝试使用 DrawingContext 绘制一个实心三角形,它在 DrawingVisual 上呈现

目前,我已经成功地使用以下 C# 代码绘制了三角形的轮廓:

private DrawingVisual CreateTriangle()
{
DrawingVisual triangle = new DrawingVisual();
using ( DrawingContext dc = triangle.RenderOpen() )
{
Pen drawingPen = new Pen(Brushes.Black,3);
dc.DrawLine(drawingPen, new Point(0, 50), new Point(50, 0));
dc.DrawLine(drawingPen, new Point(50, 0), new Point(50, 100));
dc.DrawLine(drawingPen, new Point(50, 100), new Point(0, 50));

}
return triangle;
}

我明白了:

Unfilled Triangle

我如何绘制一个三角形,除了我绘制的边框外还有一个红色填充?

Filled Triangle

最佳答案

使用 dc.DrawGeometry 创建填充形状。

编辑:这个:

private DrawingVisual CreateTriangle()
{
var triangle = new DrawingVisual();
using (DrawingContext dc = triangle.RenderOpen())
{
var start = new Point(0, 50);

var segments = new []
{
new LineSegment(new Point(50,0), true),
new LineSegment(new Point(50, 100), true)
};

var figure = new PathFigure(start, segments, true);
var geo = new PathGeometry(new [] { figure });
dc.DrawGeometry(Brushes.Red, null, geo);

var drawingPen = new Pen(Brushes.Black, 3);
dc.DrawLine(drawingPen, new Point(0, 50), new Point(50, 0));
dc.DrawLine(drawingPen, new Point(50, 0), new Point(50, 100));
dc.DrawLine(drawingPen, new Point(50, 100), new Point(0, 50));
}

return triangle;
}

附带说明一下,如果您无论如何都必须创建 PathGeometry 并且您使用了 DrawGeometry,那么您也可以对它进行描边,那么您就不需要原始的线条图。

dc.DrawGeometry(Brushes.Red, new Pen(Brushes.Black, 3), geo);

关于c# - 在 DrawingContext 中绘制一个实心三角形,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4732869/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com