这是从这个question.
我试图用的DrawingContext
,这是在渲染绘制填充的三角形 DrawingVisual
I am trying to draw a filled triangle using DrawingContext
, which is rendered on a DrawingVisual
目前,我已成功使用下面的C#code画出一个三角形的轮廓:
Currently, I have managed to draw the outline of a triangle using the following C# code:
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;
}
我得到这样的:
I get this:
我如何得出,除了日边框我画也有一个红色填充的三角形?
How do I draw a triangle that, in addition to th border I have drawn also has a red fill?
使用 dc.DrawGeometry
以创建填充的形状。
Use dc.DrawGeometry
to create filled-in shapes.
编辑:这:
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
你还不如行程太,那么你就不需要原来的线图。
On a side note, if you have to create the PathGeometry
anyway and you use DrawGeometry
you might as well stroke it too, then you don't need your original line drawings.
dc.DrawGeometry(Brushes.Red, new Pen(Brushes.Black, 3), geo);