将图像从B点旋转到A点

时间:2017-10-15 13:13:48

标签: c# math graphics

我有两点,A和B.

我需要在 C#中使用一个公式来旋转此图片库两点。

A = *,B = + ,. =图像中心

>image 1
---------------
|  *          |
|      .      |
|          +  |
---------------
>after rotate
---------------
|  *       +  |
|      .      |
|             |
---------------

>image 2
---------------
|          *  |
|      .      |
|  +          |
---------------
>after rotate
---------------
|  +       *  |
|      .      |
|             |
---------------

我找到了旋转image 1的公式,但此公式不适用于image 2。这个公式是:

float degree = -(float)(Math.Atan2(pointBy - pointAy, pointBx - pointAx) * (180 / Math.PI))

我也使用此功能旋转位图

private Bitmap RotateImage( Bitmap bmp, float angle ) {
     Bitmap rotatedImage = new Bitmap( bmp.Width, bmp.Height );
     using ( Graphics g = Graphics.FromImage( rotatedImage ) ) {
        g.TranslateTransform( bmp.Width / 2, bmp.Height / 2 ); //set the rotation point as the center into the matrix
        g.RotateTransform( angle ); //rotate
        g.TranslateTransform( -bmp.Width / 2, -bmp.Height / 2 ); //restore rotation point into the matrix
        g.DrawImage( bmp, new Point( 0, 0 ) ); //draw the image on the new bitmap
     }

     return rotatedImage;
}

https://stackoverflow.com/a/12025915/5220303

1 个答案:

答案 0 :(得分:0)

我找到了图像2的这个公式

float degree = -(float)(Math.Atan2(pointAy - pointBy, pointAx - pointBx) * (180 / Math.PI));

相关问题