【问题标题】:C# RotateTransform - problems changing centerC# RotateTransform - 改变中心的问题
【发布时间】:2016-07-06 08:21:43
【问题描述】:

在我的 C# 程序中,我使用 RotateTransform 方法来旋转我想要绘制的图片。 这已经奏效了,但我不知道如何改变图片旋转的中心点。 默认情况下,它是我的图片框的左下角,不幸的是我需要围绕 (760, 480) px 的另一个点旋转。

我到处搜索,只发现了这个 CenterX 属性。 CenterX msdn

无论如何,我似乎没有在 Visual Studio 中找到这个属性, 所以我想我做错了。

我当前的代码如下所示:

*e.Graphics.RotateTransform(angle);
e.Graphics.DrawLine(Pens.Black, physicObj.lineStartingPoint, physicObj.lineEndingPoint);
e.Graphics.FillEllipse(new SolidBrush(Color.Red), new Rectangle(physicObj.leftCornerCircle, physicObj.circleSize));
e.Graphics.FillRectangle(new SolidBrush(Color.Blue), new Rectangle(physicObj.leftCornerRectangle, physicObj.rectangleSize));*

这部分工作正常,但使用了错误的中心点来旋转。 我尝试过使用

e.Graphics.RotateTransform.CenterX = ... ;

但在 e.Graphics.RotateTransform 中似乎没有可访问的 CenterX。 Visual Studio 在 RotateTransform 下方显示一条红线,表示它是一种方法,在给定上下文中无效。 我不知道设置此属性的方法,也没有找到任何这样做的编码示例,并且根据 Microsoft 提供的信息(在链接中),我认为这是这样做的方法。

希望有人可以解释我需要做些什么来改变这个中心点。 谢谢!

【问题讨论】:

  • Draw line rotated at an angle over bitmap 的可能重复 另一个示例 here - 在排序中使用 Graphics.TranslateTranform 之前和之后,然后绘制.. - CenterX 来自媒体,即 WPF;我假设你正在做 WInForms
  • 您还需要学习一些语言基础知识。将方法视为属性是没有意义的。

标签: c# msdn rotatetransform


【解决方案1】:

这很简单:
1. 翻译到中心
2. 旋转
3. 翻译回来

float centerX = 760;
float centerY = 480;
e.Graphics.TranslateTransform(-centerX, -centerY);
e.Graphics.RotateTransform(angle);
e.Graphics.TranslateTransform(centerX, centerY);

基本上,您创建 3 个矩阵并将它们相乘以获得结果 - 单个变换矩阵,2D 和 3D 变换的基础。


附言为方便起见,您可以创建一个扩展方法:

public static class GraphicsExtensions
{
  public static void TranslateTransform(this Graphics g, float x, float y, float angle)
  {
    g.TranslateTransform(-x, -y);
    g.RotateTransform(angle);
    g.TranslateTransform(x, y);
  }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多