System.Drawing.Graphics

I have one problem regarding the rotation of an ellipse using this Center. Suppose I have one ellipse and what it needs to be to rotate this ellipse at a point set by the user, and the ellipse should rotate around this given point. I tried

g.RotateTransform(…)
g.TranslateTransform(…)

The code:

Graphics g = this.GetGraphics(); 
g.RotateTransform((float)degreeArg); //degree to rotate object 
g.DrawEllipse(Pens.Red, 300, 300, 100, 200);

this works great, but how can we let our center rotate the ellipse ....

As possible, any buddy can offer ...... Thanks .......

+3
source share
2 answers

RotateTransform always rotates around the origin. Therefore, you need to first translate your center of rotation to the beginning, then rotate, and then translate it back.

Something like that:

Graphics g = this.GetGraphics(); 
g.TranslateTransform(300,300);
g.RotateTransform((float)degreeArg); //degree to rotate object 
g.TranslateTransform(-300,-300);
g.DrawEllipse(Pens.Red, 300, 300, 100, 200);
+4
source
//center of the rotation
PointF center = new PointF(...);
//angle in degrees
float angle = 45.0f;
//use a rotation matrix
using (Matrix rotate = new Matrix())
{
    //used to restore g.Transform previous state
    GraphicsContainer container = g.BeginContainer();

    //create the rotation matrix
    rotate.RotateAt(angle, center);
    //add it to g.Transform
    g.Transform = rotate;

    //draw what you want
    ...

    //restore g.Transform state
    g.EndContainer(container);
}
+3
source

All Articles