How to get from a Shape object to a GeometryDrawing (WPF)

  • Thread starter Sebastian Daser
  • Start date
S

Sebastian Daser

Hi all,

I am trying to develop a WPF application which should show a Polygon Shape
inside an Image control. At a later point the Points of the Polygon should be
calculated by the application, so I need to create the objects in code.

I have tried the following:
public partial class ImageControl : Image
{
public ImageControl()
{
InitializeComponent();

DrawingGroup group = new DrawingGroup();

Polygon polygon = new Polygon();
polygon.Stretch = Stretch.Fill;
polygon.Fill = Brushes.LightGreen;
polygon.Stroke = Brushes.Black;
polygon.Points.Add(new Point(0, 0));
polygon.Points.Add(new Point(0, 0.5));
polygon.Points.Add(new Point(0.7, 0.7));
polygon.Points.Add(new Point(0.5, 0));

Geometry polygonGeometry = polygon.RenderedGeometry;

group.Children.Add(new GeometryDrawing(Brushes.Blue, null,
polygonGeometry));

DrawingImage image = new DrawingImage(group);

this.Source = image;
}
}

However this does not work. When replacing the line
Geometry polygonGeometry = polygon.RenderedGeometry;
with something like
Geometry polygonGeometry = Geometry.Parse("M100,125 L300, 330 L700, 260
L430, 10");
a Polygon is drawn, so I guess the RenderGeometry method does not produce
the desired result.

How can I convert a Shape like Polygon to an object of type Geometry or
GeometryDrawing?

Thanks and kind regards
Sebastian
 
F

Family Tree Mike

Sebastian Daser said:
Hi all,

I am trying to develop a WPF application which should show a Polygon Shape
inside an Image control. At a later point the Points of the Polygon should be
calculated by the application, so I need to create the objects in code.

I have tried the following:
public partial class ImageControl : Image
{
public ImageControl()
{
InitializeComponent();

DrawingGroup group = new DrawingGroup();

Polygon polygon = new Polygon();
polygon.Stretch = Stretch.Fill;
polygon.Fill = Brushes.LightGreen;
polygon.Stroke = Brushes.Black;
polygon.Points.Add(new Point(0, 0));
polygon.Points.Add(new Point(0, 0.5));
polygon.Points.Add(new Point(0.7, 0.7));
polygon.Points.Add(new Point(0.5, 0));

Geometry polygonGeometry = polygon.RenderedGeometry;

group.Children.Add(new GeometryDrawing(Brushes.Blue, null,
polygonGeometry));

DrawingImage image = new DrawingImage(group);

this.Source = image;
}
}

However this does not work. When replacing the line
Geometry polygonGeometry = polygon.RenderedGeometry;
with something like
Geometry polygonGeometry = Geometry.Parse("M100,125 L300, 330 L700, 260
L430, 10");
a Polygon is drawn, so I guess the RenderGeometry method does not produce
the desired result.

How can I convert a Shape like Polygon to an object of type Geometry or
GeometryDrawing?

Thanks and kind regards
Sebastian

I have done very little with WPF, but the sets of coordinates being used in
both your examples are very different. Are you sure the first example is not
simply drawing avery small polygon?

Mike
 
S

Sebastian Daser

Hi Mike,

Sorry to get back to you so late.

I am expecting the ImageControl to scale the drawing. In any case, if
coordinates should be the problem, then I should at least see a single pixel
in the upper left corner of the ImageControl.

Thanks and kind regards
Sebastian
 
Top