How to make DrawingVisual objects in C# have unique Pen Brush colour, but share the same thickness

132 views Asked by At

I'm building an application that can load in Vector files and display them on a Canvas. To zoom and pan efficiently I'm using DrawingVisual shapes for my background maps that don't change often.

In order to keep the line thickness the same regardless of zoom, I modify the Pen.Thickness with each zoom event.

This is working fine, but now that I'm trying to assign colours to the Pen I'm running into problems. Because all objects share the same Pen for thickness/zooming reasons, I don't know how to assign different colours to different objects. By sharing the same Pen every object is forced to be the same colour.

How can I ensure that all the shapes receive Thickness updates after a zoom event, but have unique colours per object? If I need a different Pen for every colour, there is no way of know how many pens I would need.

My code is below:

Pen myPen = new Pen();

This is my Method that produces polylines:

private DrawingVisual CreatePolyline(PointCollection points, Brush colour)
{
    DrawingVisual drawingVisual = new DrawingVisual();
    DrawingContext drawingContext = drawingVisual.RenderOpen();

    myPen.Thickness = 0.5 / Coords.zoom;
    myPen.Brush = colour;

    var geometry = new StreamGeometry();
    using (StreamGeometryContext ctx = geometry.Open())
    {
        ctx.BeginFigure(points[0], false, false);
        List<Point> _points = points.Skip(1).ToList();
        ctx.PolyLineTo(_points, true, false);
    }

    geometry.Freeze();
    drawingContext.DrawGeometry(null, myPen, geometry);
    drawingContext.Close();

    return drawingVisual;
}

This Method is triggered every time I zoom

public void Handle(ZoomEvent zoomEvent)
{
    myPen.Thickness = 0.5 / zoomEvent.Zoom;
}

This is the method that assigns colour based on what file it belongs in, layer, item itself and any overriding user choices.

private void UpdateVisualColection()
{
    visualCollection.Clear();
    foreach (MapReference reference in backgroundMaps.References)
    {
        if (reference.IsVisible == true)
        {
            foreach (ReferenceLayer layer in reference.Layers)
            {
                if (layer.IsVisible == true)
                {
                    foreach (SingleItem item in layer.SingleItems)
                    {
                        System.Windows.Media.Color colour;

                        if (reference.ColourOveride == true)
                        {
                            colour = reference.Colour;
                        }
                        else if (item.ColourByLayer == true || layer.ColourOveride == true)
                        {
                            colour = layer.Colour;
                        }
                        else
                        {
                            colour = item.ColourOveride;
                        }
                        var Brush = new SolidColorBrush(Color.FromArgb(255, colour.R, colour.G, colour.B));
                        visualCollection.Add(CreatePolyline(item.Points, Brush));
                    }

                }

            }

        }
    }
}
0

There are 0 answers