【发布时间】:2011-03-05 07:08:59
【问题描述】:
如何旋转图像然后移动到左上角 0,0 而不切断图像。
请阅读代码中的 cmets。我卡在第 3 步 我认为使用三角学应该可以解决这个问题。
谢谢
private Bitmap RotateImage(Bitmap b, float angle)
{
//create a new empty bitmap to hold rotated image
Bitmap returnBitmap = new Bitmap(b.Width, b.Height);
//make a graphics object from the empty bitmap
Graphics g = Graphics.FromImage(returnBitmap);
//STEP 1 move rotation point to top left
g.TranslateTransform((float)0, (float)0);
//STEP 2 rotate
g.RotateTransform(angle);
//STEP 3 move image back to top left without cutting off the image
//SOME trigonometry calculation here
int newY = b.Height;
g.TranslateTransform(-(float)0, -newY);
//draw passed in image onto graphics object
g.DrawImage(b, new Point(0, 0));
return returnBitmap;
}
【问题讨论】:
-
TranslateTransform(0,0)不会像您认为的那样做。你想要TranslateTransform(b.Width/2, b.Height/2)。这会将图像的中心放置在原点。然而,其余的超出了我的范围。 -
我虽然将所有内容移动到 0,0 坐标会使做正弦、余弦、正切的事情变得容易得多。我的示例不需要移动到 0,0,因为图像已经在 0,0