我有一个图像,我想按顺时针方向旋转指定的度数。我不想剪掉任何东西,所以我根据指定的旋转计算新图像的宽度和高度(例如,旋转45度需要更高更宽的图像。
//Calculate required size of new image
GraphicsPath path = new GraphicsPath();
path.AddRectangle(new RectangleF(0f, 0f, bmpSource.Width, bmpSource.Height));
Matrix matrix = new Matrix();
matrix.Rotate(iRotationDegrees);
RectangleF rctNewSize = path.GetBounds(matrix);
//Create new image
Bitmap bmpRotated = new Bitmap(Convert.ToInt32(rctNewSize.Width), Convert.ToInt32(rctNewSize.Height));
using (Graphics g = Graphics.FromImage(bmpRotated))
{
//Set rotation point to center of image
g.TranslateTransform(bmpRotated.Width / 2, 0);
g.RotateTransform(iRotationDegrees);
//Draw the rotated image on the bitmap
g.DrawImageUnscaled(bmpSource, 0,0);
}
当角度为45度时,当我将TranslateTransform设置为bmpRotated.Width / 2,0时,旋转的图像不是水平居中,左下角会被切掉一点。
我在这里缺少一些数学,正确地计算出传递给TranslateTransform的相应dx / dy值。