Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
Solved

# How to apply RotateTransform to this control?

Posted on 2010-11-16
Medium Priority
529 Views
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
0
Question by:YetAnotherCoder
[X]
###### Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

• Help others & share knowledge
• Earn cash & points

LVL 40

Expert Comment

ID: 34154627
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;
``````
0

Author Comment

ID: 34155625
Hi ged325,

I am sorry for not making it clear. It is not for silverlight. It is a Windows User Control.
0

LVL 40

Expert Comment

ID: 34155956
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.

0

LVL 32

Expert Comment

ID: 34156588
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);
}
``````
0

LVL 86

Accepted Solution

Mike Tomlinson earned 2000 total points
ID: 34159121
Add all the contents to a GraphicsPath, including an outer minimum bounding rectangle, and then use the Transform method to physically rotate all the points.  You can use a rotated GraphicsPath with just the outer minimum bounding rectangle to pass to the Region() constructor to clip the UserControl to the rotated image.

Again, this is the same stuff I've rehashed for you over and over again...  =\

Study this puppy again:
http://www.experts-exchange.com/Programming/Languages/C_Sharp/Q_26541848.html#33921826
0

Author Comment

ID: 34161248
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);

0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
###### Suggested Courses
Course of the Month6 days, 7 hours left to enroll