Click here to Skip to main content
15,878,048 members
Please Sign up or sign in to vote.
1.44/5 (2 votes)
See more:
Hello everyone...
I'm drawing a rectangle using the Rectangle Class, but I want to rotate this rectangle around its Pivot. It means it stays in its location, but is rotating - it will look like roolling.

How can I do it please?
Posted
Updated 4-Dec-17 19:03pm
v2
Comments
OriginalGriff 6-Nov-10 12:38pm    
See revised answer.

See MSDN: Graphics.RotateTransform method[^]

Basically, to rotate a rectangle by 20 degrees clockwise in your paint event:
e.Graphics.RotateTransform(20.0F);
e.Graphics.DrawRectangle(Pens.Red, new Rectangle(5, 5, 30, 40));


"thnx ..it worked...but if i want to execute this in the Timer_tick event .for every second a rotation happened .. is that possible..and How please ?? Thanx anyway"

Firstly, don't draw in your timer event. Instead, handle the Paint event for a Panel or the Form, and draw there. Then in your timer event, change the angle to want top draw at (in a class level variable) and use the Invalidate method on the Panel or Form.
C#
private float rotateAngle = 0.0F;
private void timer1_Tick(object sender, EventArgs e)
    {
    rotateAngle += 5.0F;        // Step the draw angle on
    if (rotateAngle > 360.0F)
        {
        rotateAngle = 0.0F;
        }
    MyPanelForDrawingRectanglesOn.Invalidate();
    }
I would prefer to use a panel as the drawing won't then go beneath any other controls, but it is up to you...
 
Share this answer
 
v2
Comments
chernobel 6-Nov-10 12:23pm    
thnx ..it worked...but if i want to execute this in the Timer_tick event .for every second a rotation happened .. is that possible..and How please ?? Thanx anyway
chernobel 6-Nov-10 13:22pm    
it worked very good but with a problem that the rectangle is rotating around the top left corner..
i try the method RotatAt()...to rotate around the center of the rectangle.. but it didn't work...
so how can i solve it? please.
Don't forget to make TranslateTransform if you want to rotate around other then (0,0) point.


C#
public partial class Form1 : Form
    {
        Timer timer;
        int angle;
        public Form1()
        {
            InitializeComponent();
        }
        private void Form1_Load(object sender, EventArgs e)
        {
            this.Paint += new PaintEventHandler(Form1_Paint);
            angle = 0;
            timer = new Timer();
            timer.Tick += new EventHandler(timer_Tick);
            timer.Interval = 10;
            timer.Start();
        }
        void Form1_Paint(object sender, PaintEventArgs e)
        {
            Graphics g = this.CreateGraphics();
            //the central point of the rotation
            g.TranslateTransform(100, 100);
            //rotation procedure
            g.RotateTransform(angle);
            g.DrawRectangle(Pens.Red, new Rectangle(0, 0, 50, 30));
        }
        void timer_Tick(object sender, EventArgs e)
        {
            angle++;
            this.Invalidate();
        }
    }
 
Share this answer
 
v2
Comments
Member 10671056 8-Jun-14 13:24pm    
Neither solution seems to address the problem. The poster asked how to rotate a `Rectangle Class' NOT a GDI Rectangle. I have the same question: Is there a way to rotate a Rectangle Class (from Microsoft::VisualBasic::PowerPacks::RectangleShape) in the same manner as a GDI Graphics shape.
'simple.
'use this matrix
mat = New Drawing2D.Matrix
mat.RotateAt(angle, New Point(x+ rect.Width / 2, y+ rect.Height / 2))
'y and x are points of drawing rectangle
Dim gr As Graphics= Me.CreateGraphics
gr.Transform = mat
gr.DrawRectangle(pen.color, x, y, rect.Width,rect.Height)
 
Share this answer
 
Comments
Dave Kreskowiak 10-Sep-15 23:29pm    
Don't post answers to 5 year old questions. Your answer doesn't really add anything to the discussion at all.
Member 14959834 17-Feb-23 15:31pm    
it helped me almost 8 years from his solution and 13 years after question...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900