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

发布于 2024-12-16 20:56:59 字数 944 浏览 2 评论 0原文

我已经尝试过这个:

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 对象作为图形,并且我有一个 计时器 女巫只调用 this.Refresh();

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

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

浪漫人生路 2024-12-23 20:56:59

您可以尝试使用带有 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() 会将图形恢复到正常处理。

You can try using a matrix with the RotateAt method to center the rotation around the rectangle:

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文