Link to home
Start Free TrialLog in
Avatar of YetAnotherCoder
YetAnotherCoder

asked on

How to apply RotateTransform to this control?

The following control is very simple to draw when XY co-ordinates are in Top Left of the screen. But what if I want the control to rotate left or right. Please do understand that I would like to have the region set to the drawn portions, to clip any unwanted portions of the area.

Thank you User generated image
Avatar of Kyle Abrahams, PMP
Kyle Abrahams, PMP
Flag of United States of America image

Is this for silverlight?

RotateTransform rt = new RotateTransform();

//rotate 90 clockwise
rt.Angle = 90;
//normal rotation is top left corner.  Setting to 0.5,0.5 rotates about the center.
rt.CenterX = 0.5;
rt.CenterY = 0.5;

myBmp.RenderTransform = rt;

Open in new window

Avatar of YetAnotherCoder
YetAnotherCoder

ASKER

Hi ged325,

I am sorry for not making it clear. It is not for silverlight. It is a Windows User Control.
Unfortunately looks like you'll need WPF, which the above should apply for.  It doesn't look like it's possible in a normal windows application.

http://social.msdn.microsoft.com/Forums/en-SG/winforms/thread/38a6ecba-1b54-41c7-a3df-20670798c3df
Give this a shot.

using System.Drawing.Drawing2D;
private void Form1_Paint(object sender, PaintEventArgs e)
		{
			Graphics g = e.Graphics;

			Pen pen = new Pen(Color.Black);

			g.RotateTransform(45.0f);  // rotate 45'
			g.TranslateTransform(this.ClientSize.Width / 2, this.ClientSize.Height  / 2, MatrixOrder.Append);           // translate

			g.DrawRectangle(pen,new Rectangle(-100, -40, 200, 80));
			g.DrawEllipse(pen, -100, -40, 200, 80);
		}

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Mike Tomlinson
Mike Tomlinson
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Hi Eric,

Thanks for the reply. But what I have to do is this:

Quite similar to what you said but I have to set the region to the drawn area so that not drawn area will get clipped. So I have to do the following:

In addition to code of yours, I have to do this

Matrix M = new Matrix();
            // Translate the Origin to Center of the control in the matrix
            M.Translate(ClientRectangle.Width / 2, ClientRectangle.Height / 2);
            // If needs to be rotated then apply rotation as well for the above matrix
            if (SprayDirection == Spray_Direction.Left)
                M.RotateAt(90f, Center);
            else if (SprayDirection == Spray_Direction.Right)
                M.RotateAt(-90f, Center);
            // Re-compute all the graphics path points to the newly created matrix order
            gp.Transform(M);
            // Apply to the region
            Region rgn = new Region(gp);