tags:

views:

35

answers:

2

Hi,

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.Clone();
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

A: 

Add this before the last line in your code

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

Hope this helps!!

viky
Thanks Anurag. This helped me to get a circle on the canvas but not the intended one. I wanted something like,http://i.msdn.microsoft.com/ms746682.mil_task_combined_geometry_union%28en-us,VS.90%29.png
subho100
A: 

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);
Andy Dent
Thanks! It was a perfect solution.
subho100