Flipped copy of a Rectangle using ScaleTransform in WPF <p>I am trying to create a flipped copy of a rectangle in Left, Right, Up and Down directions.</p> <p>However I am able to do it when the selected rectangle has no prior transformation applied on it.</p> <p>But once a rectangle is being transformed with <code>ScaleTransform</code> and I try to flip it again in the desired direction, the solution does not work for me.</p> <p>Please correct me what I am doing wrong here.</p> <p>Here is the code I am using</p> <p><strong>XAML</strong></p> <pre><code>&lt;Grid&gt; &lt;Grid.ColumnDefinitions&gt; &lt;ColumnDefinition&gt;&lt;/ColumnDefinition&gt; &lt;ColumnDefinition Width="10"&gt;&lt;/ColumnDefinition&gt; &lt;ColumnDefinition Width="Auto"&gt;&lt;/ColumnDefinition&gt; &lt;/Grid.ColumnDefinitions&gt; &lt;Canvas Name="canvas" Background="LightGray" MouseDown="Canvas_MouseDown"&gt; &lt;Rectangle x:Name="rect" StrokeThickness="2" Stroke="Black" Height="60" Width="100" Canvas.Left="500" Canvas.Top="300" MouseDown="rect_MouseDown"&gt; &lt;Rectangle.Fill&gt; &lt;LinearGradientBrush StartPoint="0.5,0" EndPoint="0.5,1"&gt; &lt;GradientStop Color="#FFFB7D0E" Offset="0" /&gt; &lt;GradientStop Color="#FF59C103" Offset="1" /&gt; &lt;GradientStop Color="#FFFFFFFF" Offset="0.51" /&gt; &lt;/LinearGradientBrush&gt; &lt;/Rectangle.Fill&gt; &lt;/Rectangle&gt; &lt;/Canvas&gt; &lt;StackPanel Grid.Column="2"&gt; &lt;Button Content="Left" Click="Button_Click"&gt;&lt;/Button&gt; &lt;Button Content="Right" Click="Button_Click"&gt;&lt;/Button&gt; &lt;Button Content="Up" Click="Button_Click"&gt;&lt;/Button&gt; &lt;Button Content="Down" Click="Button_Click"&gt;&lt;/Button&gt; &lt;/StackPanel&gt; &lt;/Grid&gt; </code></pre> <p><strong>C#</strong></p> <pre><code> Rectangle selectedRect; //Flip private void Button_Click(object sender, RoutedEventArgs e) { if (selectedRect == null) return; //Create a copy of rectangle string rectXaml = XamlWriter.Save(selectedRect); StringReader stringReader = new StringReader(rectXaml); XmlReader xmlReader = XmlReader.Create(stringReader); Rectangle newRect = (Rectangle)XamlReader.Load(xmlReader); //Calculate the bounding box var boundingRect = newRect.RenderTransform.TransformBounds(new Rect(0, 0, newRect.Width, newRect.Height)); double cX, cY, sX, sY; sX = 1; sY = 1; cX = (boundingRect.Left + boundingRect.Right) / 2; cY = (boundingRect.Top + boundingRect.Bottom) / 2; switch ((sender as Button).Content.ToString()) { case "Up": sX = 1; sY = -1; cY = boundingRect.Top; break; case "Down": sX = 1; sY = -1; cY = boundingRect.Bottom; break; case "Left": sX = -1; sY = 1; cX = boundingRect.Left; break; case "Right": sX = -1; sY = 1; cX = boundingRect.Right; break; } newRect.Stroke = Brushes.Black; newRect.RenderTransform = new ScaleTransform(sX, sY, cX, cY); newRect.MouseDown += new MouseButtonEventHandler(rect_MouseDown); //Add new rect to Canvas canvas.Children.Add(newRect); } //To select a rectangle on canvas private void rect_MouseDown(object sender, MouseButtonEventArgs e) { if (selectedRect != null) selectedRect.Stroke = Brushes.Black; selectedRect = sender as Rectangle; selectedRect.Stroke = Brushes.Blue; e.Handled = true; } //clear the selection private void Canvas_MouseDown(object sender, MouseButtonEventArgs e) { if (selectedRect != null) { selectedRect.Stroke = Brushes.Black; selectedRect = null; } e.Handled = true; } </code></pre>