Reputation: 61
I am having trouble when trying to obtain the actual width of a transformed ( rotated ) element in WPF. I have a rectangle containing an image, in a Canvas. When I rotate the rectangle, I would like to get the rendered width to center it in my window, but I always get the base Width, no matter how I try.
I tried using Width, ActualWidth, RenderSize.Width : exact same result. I tried using a LayoutTransform and RenderTransform, same.
I really don't get what I'm doing wrong, everything I've read about the subject suggest that I should get the rendered width of the whole bounding box using ActualWidth, and not the default width of my rectangle/image.
Here is my code :
Setting up the scaleTransform :
private ScaleTransform scaleT = new ScaleTransform();
private RotateTransform rotateT = new RotateTransform();
private TransformGroup tGroup = new TransformGroup();
//Adding renderTransform
scaleT.ScaleX = 1;
scaleT.ScaleY = 1;
rotateT.Angle = 0;
tGroup.Children.Add(rotateT);
tGroup.Children.Add(scaleT);
imageContainer.RenderTransform = tGroup;
And the rotation :
private void rotateWindow(string direction)
{
double prevWidth = this.Width;
double prevHeight = this.Height;
this.Width = prevHeight;
this.Height = prevWidth;
imageContainer.RenderTransformOrigin = new Point(0.5, 0.5);
if (direction == "Right")
{
rotateT.Angle += 30;
}
if (direction == "Left")
{
rotateT.Angle -= 30;
}
adaptWindowToGrid();
}
Thanks !
Upvotes: 2
Views: 1783