如何为椭圆和直线创建CombinedGeometry

时间:2018-08-28 16:04:22

标签: c# wpf

我想画椭圆,但不画十字。我怀疑我需要使用不透明蒙版。这就是我试图做到的方式。

Color grey = Color.FromArgb(128, Colors.Gray.R, Colors.Gray.G, Colors.Gray.B);
double radius = Math.Min(ActualWidth, ActualHeight) / 2;
Brush ellipse_brush = new SolidColorBrush(grey);

CombinedGeometry cg = new CombinedGeometry();

Drawing maskDrawing = new GeometryDrawing(Brushes.Lime, null, cg);
DrawingBrush mask = new DrawingBrush(maskDrawing);

dc.PushOpacityMask(mask);
dc.DrawEllipse(ellipse_brush, new Pen(ellipse_brush, 0), new Point(radius, radius), radius, radius);
dc.Pop();

问题是我不了解如何为椭圆和两行创建CombinedGeometry。还是我走错了路?

1 个答案:

答案 0 :(得分:2)

您不需要与CombinedGeometry结合使用的不透明蒙版。

GeometryGroup用两条线和适当的Pen创建交叉轮廓几何图形,然后将Xor与EllipseGeometry组合起来并绘制结果:

var radius = Math.Min(ActualWidth, ActualHeight) / 2;
var crossSize = 0.8 * radius;
var crossThickness = 0.3 * radius;
var centerPoint = new Point(radius, radius);
var ellipseGeometry = new EllipseGeometry(centerPoint, radius, radius);
var crossGeometry = new GeometryGroup();

crossGeometry.Children.Add(new LineGeometry(
    new Point(centerPoint.X - crossSize / 2, centerPoint.Y - crossSize / 2),
    new Point(centerPoint.X + crossSize / 2, centerPoint.Y + crossSize / 2)));
crossGeometry.Children.Add(new LineGeometry(
    new Point(centerPoint.X - crossSize / 2, centerPoint.Y + crossSize / 2),
    new Point(centerPoint.X + crossSize / 2, centerPoint.Y - crossSize / 2)));

var crossPen = new Pen
{
    Thickness = crossThickness,
    StartLineCap = PenLineCap.Round,
    EndLineCap = PenLineCap.Round
};

var crossOutlineGeometry = crossGeometry.GetWidenedPathGeometry(crossPen);
var combinedGeometry = new CombinedGeometry(GeometryCombineMode.Xor,
                                            ellipseGeometry, crossOutlineGeometry);

dc.DrawGeometry(Brushes.Gray, null, combinedGeometry);