简体   繁体   English

如何使用Graphics对象在特定角度旋转RectangleF?

[英]How can I rotate an RectangleF at a specific degree using Graphics object?

I have tried this: 我试过这个:

g.RotateTransform(degrees);

But nothing happens.I have one graphics object and one rectangle object witch im drawing using this method: 但没有任何反应。我有一个图形对象和一个矩形对象使用此方法绘图:

g.FillRectangle(new TextureBrush(Image.FromFile(@"D:\LOVE&LUA\Pictures\yellowWool.png")), rectangle);

And i need to rotate the rectangle somehow and draw it again. 我需要以某种方式旋转矩形并再次绘制它。

Answer with code sample please and with a simple explanation. 请用代码示例回答并简单解释一下。

EDIT: Here is the actual code I'm using: 编辑:这是我正在使用的实际代码:

        public void Draw(Graphics g,PointF location,Color clearColor)
    {
        rectangle.Location = location;
        g.Clear(clearColor);
        g.RotateTransform(10);
        //g.FillRectangle(new SolidBrush(Color), rectangle);
        g.FillRectangle(new TextureBrush(Image.FromFile(@"D:\LOVE&LUA\Pictures\yellowWool.png")), rectangle);
    }

Each frame I call this function and I'm using form's Paint event's e.Graphics object for the Graphics and i have a timer witch only calls this.Refresh(); 每个框架我调用这个函数,我正在使用窗体的Paint事件的e.Graphics对象的图形,我有一个timer只会调用this.Refresh();

EDIT 2: OK I have played a little with the transformations and g.RotateTransform rotates the whole cordinate system of the graphycs object and i need to rotate only the rectangle without changing the cordinate system 编辑2:好的我已经玩了一些转换和g.RotateTransform旋转graphycs对象的整个坐标系统我需要只旋转矩形而不改变坐标系

You can try using a matrix with the RotateAt method to center the rotation around the rectangle: 您可以尝试使用带有RotateAt方法的矩阵来围绕矩形居中旋转:

using (Matrix m = new Matrix()) {
  m.RotateAt(10, new PointF(rectangle.Left + (rectangle.Width / 2),
                            rectangle.Top + (rectangle.Height / 2)));
  g.Transform = m;
  using (TextureBrush tb = new TextureBrush(Image.FromFile(@"D:\LOVE&LUA\Pictures\yellowWool.png"))
    g.FillRectangle(tb, rectangle);
  g.ResetTransform();
}

The ResetTransform() will turn the graphics back to normal processing after that. 之后, ResetTransform()会将图形恢复为正常处理。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM