.:: CODE SNIPPET ::.

"Your time is limited, so don't waste it living someone else's life"

How to rotate a rectangle in WPF


When you want to rotate a rectangle in WPF.
You can use the RenderTranform attribute of Rectangle.
I give you an simple example on how to rotate a rectangle as following
First I have a rectangle object named rect. You initiate this object like:

Rectangle rect = new Rectangle();
rect.Fill = new SolidColorBrush(Colors.Blue);
rect.Width = 100;
rect.Height = 100;
Canvas.SetLeft(rect, 50);
Canvas.SetTop(rect, 50);


Now the rest work is you indicate the point on the Rectangle the rotation point. In such case, I want it is the center point of the rectangle which has the abscissa and the ordinate are in turn the distance from the top and the left of the Rectangle.

In detail, they have value 50 and 50
we rotate the Rectangle by the following code:

rect.RenderTransform = new RotateTransform(angle, disX, disY);

Here:
+ angle: the angle value from the original position (pre rotation) to the current position (post rotation).
+ disX equals 50
+ disY equals 50

Advertisements

2 responses to “How to rotate a rectangle in WPF

  1. tin tuc July 9, 2013 at 3:12 AM

    These are really great ideas in regarding blogging.
    You have touched some fastidious points here. Any way keep up wrinting.

  2. Hoàng Minh July 9, 2013 at 12:37 PM

    tin tuc :

    These are really great ideas in regarding blogging.
    You have touched some fastidious points here. Any way keep up wrinting.

    Could you mind showing me the fastidious points you mentioned?

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: