C#旋转(转换)问题

C# rotation (transformation) issue

我正在尝试创建程序,其中将在 canvas 上绘制鱼类列表。 (下一步将根据一些计算改变鱼的位置) 每条鱼都由位图表示(带有鱼图片的 png 文件 7x12 像素)。

我在上面创建了 Form,PictureBox 是我的绘图 canvas。它的尺寸为 640x480 像素。

下面的代码是我使用的简化代码(我切断了所有不必要的东西)。我的问题是转换矩阵,目前只有旋转。

问题classFishmethod Draw() where我正在尝试进行转换,目前我将每条鱼的旋转角度设置为 30 度,但稍后每条鱼都会有不同的起始旋转角度。我想对鱼将围绕其中心的旋转角度进行旋转的位置进行转换。所以在这种情况下,所有的鱼都应该排成一行,并且每条鱼都按其旋转角度(这里是 30 度)旋转。
但是它们被放在对角线上,所以转换不知何故搞砸了。 我怎样才能解决这个问题? 我可能错误地使用了转换。

在 classes

中使用的命名空间
using System.Drawing;//Graphics, Point
using System.Drawing.Drawing2D;//Matrix

    class Fish {
      public Point position;
      public int rotation;
      public Graphics g;
      public Image fishImage;
      private Rectangle rect;
      private Matrix matrix;

      public Fish(ref Graphics g, int x, int y, int rotation,Image img){
        this.g =  g;
        position = new Point(x,y);
        this.rotation = rotation;
        this.fishImage = img;
        this.rect = new Rectangle(position.X,position.Y, fishImage.Width, fishImage.Height);

      }

   public void Draw() {
    matrix = new Matrix();
    matrix.Rotate((float)rotation, Matrix.Append); //if i comment this it
    //will be drawn in one line
    //according to the initial values for position
    //if i let the rotation here it will be on diagonale
    //i want it on one line but rotated
    g.Transform = matrix;

    rect = new Rectangle(position.X, position.Y, fishImage.Width, fishImage.Height);
    g.DrawImage(fishImage, rect);
   }
}//end Fish class

表格

public partial class Form1 : Form
{
 private Bitmap canvasBitmap; //bitmap for drawing
 private Graphics g;          
 Image fishImage;

 private List<Fish> fishes = new List<Fish>();
  public Form1() {
    InitializeComponent();
   //png image 7x12 pixels 
   fishImage = FishGenetic.Properties.Resources.fishImage; 
   //on Form there is placed PictureBox called canvas
   //so canvas is PictureBox 640x480 px
   canvasBitmap = new Bitmap(canvas.Width, canvas.Height);
   canvas.Image = canvasBitmap;

   //prepare graphics
   g = Graphics.FromImage(canvasBitmap);
   g.SmoothingMode = SmoothingMode.AntiAlias;

   InitFishes();
   DrawFishes();
   canvas.Invalidate(); //invalidate the canvas 

  }//end Form1 constructor     


 private void InitFishes() {

   Fish fish1 = new Fish(ref g, 10, 10, 30, fishImage);
   Fish fish2 = new Fish(ref g, 20, 10, 30, fishImage);
   Fish fish3 = new Fish(ref g, 30, 10, 30, fishImage);
   Fish fish4 = new Fish(ref g, 40, 10, 30, fishImage);
   Fish fish5 = new Fish(ref g, 50, 10, 30, fishImage);
   fishes.Add(fish1);
   fishes.Add(fish2);
   fishes.Add(fish3);
   fishes.Add(fish4);
   fishes.Add(fish5);
 }

private void DrawFishes() {
   foreach(Fish fish in fishes) {
       fish.Draw();
   }
}

}//end Form1 class

主要class

static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Form1());
        }
    }

您应该使用 RotateAt 而不是 Rotate。 Rotate 方法围绕控件的左上角(原点)旋转鱼,而 RotateAt 围绕您指定的点旋转某些东西。

只需计算每条鱼的中心(X=左+FishWidth/2,Y=上+FishHeight/2)并围绕该点旋转。