问题
We have a StreamGeometry object which we would like to render in about 400 different locations during the OnRender call. The problem is, of course, that a geometry object uses absolute coordinates.
While we could of course apply transforms before the render call, that means we'd in essence be creating 400 transforms as well, which just seems like overkill.
We just want to say 'Render this in that location, like this (Note: DrawGeometryAtPoint is fictitious)...
protected override void OnRender(System.Windows.Media.DrawingContext dc)
{
base.OnRender(dc);
var myGeometry = new StreamGeometry();
// Code to init the geometry goes here
// Render the same geometry but at four different locations
dc.DrawGeometryAtPoint(Brush1, Pen1, myGeometry, Origin1);
dc.DrawGeometryAtPoint(Brush2, Pen2, myGeometry, Origin2);
dc.DrawGeometryAtPoint(Brush3, Pen3, myGeometry, Origin3);
dc.DrawGeometryAtPoint(Brush4, Pen4, myGeometry, Origin4);
}
So can it be done?
回答1:
This is essentially the same question as your previous one.
Either you push a separate transform before each rendering.
var transform = new TranslateTransform(origin.X, origin.Y);
transform.Freeze();
dc.PushTransform();
dc.DrawGeometry(brush, pen, geometry;
dc.Pop();
This is essentially the same as putting a GeometryDrawing in a DrawingGroup and set the DrawingGroup.Transform property.
Or you put the StreamGeometry into a GeometryGroup and set the Transform there.
var transform = new TranslateTransform(origin.X, origin.Y);
transform.Freeze();
var group = new GeometryGroup { Transform = transform };
group.Children.Add(geometry);
dc.DrawGeometry(brush, pen, group;
As i told you in my comment to the other question, there is no way to get around having a separate Transform object for every rendering of the same Geometry at different locations.
EDIT: You should consider a different design. Instead of running a complete OnRender pass each time your objects move a bit, you should render once and afterwards only change the Tranform objects. Which must then of course not be frozen. Therefore you would not override OnRender in some control, but provide a special control that hosts a DrawingVisual.
来源:https://stackoverflow.com/questions/13919970/can-you-render-a-streamgeometry-object-in-multiple-places-during-an-onrender-ove