2

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 !

  • Does the transformation work correctly? – Brandon Kramer Mar 10 '17 at 13:14
  • Yes the transformation works as intended, the only problem being that the image doesn't center correctly because I use the incorrect width/height to do it automatically. – SetsunaDilandau Mar 10 '17 at 15:30
  • The properties you are mentioning don't take RenderTransform into account. Please refer to Brendan Clark's answer on the MSDN for for information about this: https://social.msdn.microsoft.com/Forums/vstudio/en-US/5b7fbb2e-717c-44e2-8468-dcdf0a4ea28d/actualwidthheight-not-changed-by-rendertransform?forum=wpf. Bottom line: "The better question is this: what are you trying to accomplish that requires you know the size of your content as it shows up on-screen? There's probably a better way to go about solving your problem than trying to calculate these bounds." – mm8 Mar 10 '17 at 15:31

0 Answers0