3

I am trying to apply RotateTransform to a Rect object with the following code.

Rect transformed = this.Rectangle;
transformed.Transform(this.rotateTransform.Value);
DrawingVisual visual = new DrawingVisual();
DrawingContext context = visual.RenderOpen();
context.DrawRectangle(null, new Pen(Brushes.Blue, 2), transformed);
context.Close();
canvas.Children.Add(visual);

but the rectangle is not rotated. However when I push the transformation to the DrawingContext, as in the following code, rectangle is transformed correctly.

Rect transformed = this.Rectangle;
DrawingVisual visual = new DrawingVisual();
DrawingContext context = visual.RenderOpen();
context.PushTransform(this.rotateTransform);
context.DrawRectangle(null, new Pen(Brushes.Blue, 2), transformed);
context.Pop();
context.Close();
canvas.Children.Add(visual);

Is there a way to transform a Rect as in the first code fragment with Rect.Transform(Matrix) function?

1 Answer 1

2

Prabably because your rotation is not multiple of 90deg. Transform on Rect results in rect that it parallel to x & y axis!

  • First case renders modified rectangle that is not rotated a syou desire - seems to be bouding box after rotation
  • Second case doest what you wish - it first "rotate content", then draws rectangle
Sign up to request clarification or add additional context in comments.

1 Comment

Yes first case draws a bounding box of the rotated rectangle in the second case. So I guess rotating a Rect as in first case is not possible.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.