c# - 如何使用 Graphics 对象以特定度数旋转 RectangleF?

标签 c# .net rotation transformation

我试过这个:

g.RotateTransform(degrees);

但是没有任何反应。我有一个图形对象和一个矩形对象,我正在使用这种方法绘制:

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

我需要以某种方式旋转矩形并再次绘制它。

请用代码示例和简单的解释回答。

编辑:这是我正在使用的实际代码:

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

每一帧我都调用这个函数,我使用表单的 Paint 事件的 e.Graphics 对象作为图形,我有一个 timer 女巫只调用 this .Refresh();

编辑 2: 好的,我玩了一些变换,g.RotateTransform 旋转了 graphycs 对象的整个坐标系,我只需要旋转矩形而不改变坐标系

最佳答案

您可以尝试使用带有 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();
}

之后 ResetTransform() 会将图形恢复为正常处理。

关于c# - 如何使用 Graphics 对象以特定度数旋转 RectangleF?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8189383/

相关文章:

android - 快速旋转手机180度,相机预览倒转

c# - 如何拍照并通过电子邮件发送?

c# - 从 .NET Core 2.2 升级到 3.1 时无法禁用默认 API 版本控制程序

c# - C# 和 PowerShell 中的非统一 System.DateTime.Now 默认格式

c# - 为什么创建新数组会抛出 OutOfMemoryException?

c++ - OpenCV : algorithm for simple image rotation and reduction

c# - 一行异步方法 SynchronizationContext

c# - C#中的整型引用类型是什么?

c# - 合并与空字符串连接

android - 具有工作选择、光标位置等的自定义旋转 EditText View