C# 系统图

C# 系统图,c#,winforms,graphics,C#,Winforms,Graphics,我有一个问题,关于用给定的中心旋转椭圆, 假设我有一个椭圆,那个么应该是通过用户给定的点旋转那个椭圆,椭圆应该围绕那个给定的点旋转。 我试过了 g.RotateTransform(…) g.TranslateTransform(…) 代码: Graphics g = this.GetGraphics(); g.RotateTransform((float)degreeArg); //degree to rotate object g.DrawEllipse(Pens.Red, 300, 3

我有一个问题,关于用给定的中心旋转椭圆, 假设我有一个椭圆,那个么应该是通过用户给定的点旋转那个椭圆,椭圆应该围绕那个给定的点旋转。 我试过了

g.RotateTransform(…)
g.TranslateTransform(…)
代码:

Graphics g = this.GetGraphics(); 
g.RotateTransform((float)degreeArg); //degree to rotate object 
g.DrawEllipse(Pens.Red, 300, 300, 100, 200);
这很好,但我们如何给出旋转椭圆的出中心

怎么可能请任何朋友推荐……
谢谢……

旋转Transform始终围绕原点旋转。所以你需要先将旋转中心平移到原点,然后旋转,然后再将其平移回来

大概是这样的:

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);

向我们展示迄今为止的代码。Graphics g=this.GetGraphics();g、 旋转变换((浮动)度)//旋转物体的角度g.drawerlipse(Pens.Red,300300100200);这很好,但是我们如何才能给出旋转椭圆的中心呢……你应该编辑你的问题以包含代码,而不是将其作为注释发布。这次已经为您完成了。谢谢,下次我会记住您的评论……现在我找到了解决方案,解决方案是//旋转点的中心f中心=新的点f(…)//以度为单位的角度浮动角度=45.0f//使用(矩阵旋转=新矩阵()){//用于还原g.变换以前的状态图形容器=g.BeginContainer();//创建旋转矩阵旋转.旋转(角度,中心);//将其添加到g.Transform g.Transform=rotate;//绘制所需内容……//还原g.Transform状态g.EndContainer(container);}@pritesh,我有向后转换的顺序-请参见我的编辑
//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);
}