开发者

How to merge two dynamically created objects overlapping each other?

Please find my code below. I tried this but didn't succeed. Any help?

Path e1 = new Path();
Path e2 = new Path();

e1.Data = new EllipseGeometry(new Rect(new Size(100, 100)));
e1.RenderTransform = new TranslateTransform(100, 100);
e1.Fill = Brushes.Transparent;
e1.Stroke = Brushes.Black;

e2.Data = new EllipseGeometry(new Rect(new Size(120, 120)));
e2.RenderTransform = new TranslateTransform(140, 140);
e2.Fill = Brushes.Transparent;
e2.Stroke = Brushes.Black;

Path p = new Path();

CombinedGeometry c1 = new CombinedGeometry();
Geometry g1 = e1.Data.Clo开发者_如何学运维ne();
Geometry g2 = e2.Data.Clone();
c1.GeometryCombineMode = GeometryCombineMode.Union;

p.Stroke = Brushes.Black;
p.StrokeThickness = 1;
p.Data = c1;

canvasMain.Children.Add(p);

Regards / subho100


You made two mistakes:

The first was assuming that the transforms would alter the location of the geometry as combined - my testing shows that they are ignored so I've used the other Rect constructor which takes a Point for the location.

The second was a more fundamental mistake which Anurag corrected – you weren't actually putting your geometry into the CombinedGeometry. I solved it a different way using the constructor as shown below.

Path e1 = new Path();
Path e2 = new Path();

e1.Data = new EllipseGeometry(new Rect(new Point(100,100), new Size(100, 100)));
e1.Fill = Brushes.Transparent;
e1.Stroke = Brushes.Black;

e2.Data = new EllipseGeometry(new Rect(new Point(140, 140), new Size(120, 120)));
e2.Fill = Brushes.Transparent;
e2.Stroke = Brushes.Black;

Path p = new Path();

CombinedGeometry c1 = new CombinedGeometry(GeometryCombineMode.Union, e1.Data, e2.Data);

p.Stroke = Brushes.Black;
p.StrokeThickness = 1;
p.Data = c1;

canvasMain.Children.Add(p);


Add this before the last line in your code

c1.Geometry1 = g1;
c1.Geometry2 = g2;

Hope this helps!!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜