C# 在位图上以一定角度旋转绘制线
我在下面的代码中画了一条从png图像中心到顶部的线:C# 在位图上以一定角度旋转绘制线,c#,.net,bitmap,gdi,C#,.net,Bitmap,Gdi,我在下面的代码中画了一条从png图像中心到顶部的线: private string ProcessImage(string fileIn) { var sourceImage = System.Drawing.Image.FromFile(fileIn); var fileName = Path.GetFileName(fileIn); var finalPath = Server.MapPath(@"~/Output/" + fi
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度处绘制
请给我指引正确的方向
谢谢假设您在
浮动角度中具有所需的角度
只需在绘制线之前插入这三条线即可:
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()