Is there an easy way of translating the PathGeometry in WPF (C#)?
Having a PathGeometry (System.Windows.Media) and a Vector (System.Windows.Vector) i would like to translate the whole Geometry without needing to analyze each single Figure with their Segments and the corresponding Points. The code is pretty straight forward but i'm looking for an approach that uses standard functionality already provided by WPF (C#). Any ideas?
Set the Geometry's Transform property to an appropriate TranslateTransform, e.g.
Geometry geometry = ...
Vector translation = ...
geometry.Transform = new TranslateTransform(translation.X, translation.Y);
After looking around a bit more I found the solution to my own question:
public PathGeometry TranslatePathGeometry(PathGeometry originalGeometry, Vector translationVector)
{
originalGeometry.Transform = new TranslateTransform(translationVector.X, translationVector.Y);
return PathGeometry.CreateFromGeometry(originalGeometry);
}
I found other options using the Geometry.Combine method, but this solution i think is more elegant.
As explained by Clemens it's not even necessary to create a new GeometryPath, you can just apply the TranslateTransform and it will work perfectly.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With