绘制线以一定的角度超过位图
问题描述:
我绘制从PNG图像的中心的线到顶部在下面的代码旋转:绘制线以一定的角度超过位图
private string ProcessImage(string fileIn)
{
var sourceImage = System.Drawing.Image.FromFile(fileIn);
var fileName = Path.GetFileName(fileIn);
var finalPath = Server.MapPath(@"~/Output/" + fileName);
int x = sourceImage.Width/2;
int y = sourceImage.Height/2;
using (var g = Graphics.FromImage(sourceImage))
{
g.DrawLine(new Pen(Color.Black, (float)5), new Point(x, 0), new Point(x, y));
}
sourceImage.Save(finalPath);
return @"~/Output/" + fileName;
}
这工作正常,我有一条线,是90度的图像的中心。 现在我需要的不是90度垂直线,我想接受用户输入的程度。如果用户输入45度,则应该从png图像中心以45度画线。
请引导我在正确的方向。
谢谢
答
让我们假设你有一个float angle
所有你需要做的是画线之前插入这三行想要的角度:
g.TranslateTransform(x, y); // move the origin to the rotation point
g.RotateTransform(angle); // rotate
g.TranslateTransform(-x, -y); // move back
g.DrawLine(new Pen(Color.Black, (float)5), new Point(x, 0), new Point(x, y));
如果你想绘制更多的东西,而不需要轮流致电g.ResetTranform()
!