如何在 .Net 中绘制有向线?
关于如何在 .Net 中绘制有向线有什么建议吗?我目前正在使用 Graphics.DrawLine 和带破折号的笔。但破折号指向两个方向。我希望它们都指向一个方向。
using (var p = new Pen(Color.FromArgb(190, Color.Green), 5))
{
p.StartCap = LineCap.Round;
p.EndCap = LineCap.ArrowAnchor;
p.CustomEndCap = new AdjustableArrowCap(3, 3);
p.DashStyle = DashStyle.Dash;
p.DashCap = DashCap.Triangle;
e.Graphics.DrawLine(p, startPt, pt);
}
Any suggestions on how to draw a directed line in .Net? I'm currently using Graphics.DrawLine with a Pen with dashes. But the dashes point in both directions. I want them to all point in a single direction.
using (var p = new Pen(Color.FromArgb(190, Color.Green), 5))
{
p.StartCap = LineCap.Round;
p.EndCap = LineCap.ArrowAnchor;
p.CustomEndCap = new AdjustableArrowCap(3, 3);
p.DashStyle = DashStyle.Dash;
p.DashCap = DashCap.Triangle;
e.Graphics.DrawLine(p, startPt, pt);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这可能不是很有帮助,但是我们在我参与的其他项目中完成此类事情的方法是实现一个自定义“笔”,它接受要绘制的线,计算实际的破折号,然后绘制每个破折号都是带有“真实”笔的单独线条。如果您要做这样的事情,那么您实际上可以将“笔”级 StartCap 和/或 EndCap 应用于每个破折号。
自定义“笔”必须接受破折号/间隙长度的数组或序列(或者可能是标准笔样式的枚举,这些样式将在内部解释为特定的破折号/间隙长度)。
许多年前,我们使用基于 GDI 的绘图系统做到了这一点,我们必须支持比 GDI 支持的内置线条样式更多的线条样式。
这是一个有趣的练习,但它也可能比它的价值更麻烦,除非你真的必须按照你所描述的方式绘制破折号。
[编辑]
如果您愿意的话,这里有一些示例代码,说明您可以如何执行此操作。
第一个是 Graphics 对象的扩展方法。它需要一支笔、虚线长度、间隙长度、起点和终点。它沿着线 p1->p2 进行插值,绘制“dash”长度段,然后跳过“gap”长度段。传入的笔应该是实心的,带有箭头端盖(以达到您要求的效果)。
代码相当粗糙,但行为或多或少是这样的:
如果输入线比总破折号+间隙长度短,则使用输入笔按原样绘制该线。
如果输入线长于总的破折号+间隙长度,则绘制“破折号”长度的线,直到该线的“剩余部分”小于破折号+间隙长度,此时剩余部分按原样绘制输入笔。
如果您想实现路径绘制,那么您必须在中间顶点周围进行插值(除非您想节省成本并独立计算每个线段的破折号)。
您可以这样称呼它:
我并不声称该代码很棒(甚至不一定很好;-),但如果您确实对绘制有向线感兴趣,它应该为您提供一个良好的起点。
祝你好运!
This might not be very helpful, but the way that we accomplished this type of thing on other projects that I have been involved with is to implement a custom "pen" that accepts the line to be drawn, calculates the actual dashes, and then draws each dash as an individual line with a "real" Pen. If you were to do something like this, then you could actually apply a "pen" level StartCap and/or EndCap to each dash.
The custom "pen" would have to accept an array or sequence of dash/gap lengths (or maybe an enumeration of standard pen styles that would be interpreted internally as specific dash/gap lengths).
We did this many years ago with a GDI-based drawing system where we had to support more line styles than the built in line styles that GDI supported.
It is an interesting exercise, but it is also probably more trouble than it is worth unless you REALLY have to have the dashes drawn as you describe.
[EDIT]
Here is some sample code for how you might do this, if you are so inclined.
First is an extension method on the Graphics object. It takes a pen, dash length, gap length, start point, and end point. It interpolates along the line p1->p2, drawing a "dash" length segment and then skipping a "gap" length segment. The passed-in pen should be solid with an arrow endcap (to achieve the effect you were asking for).
The code is pretty rough, but the behavior is, more or less, like this:
If the input line is shorter then the total dash+gap length, then the line is drawn as-is using the input pen.
If the input line is longer than the total dash+gap length, then "dash" length lines are drawn until the "remainder" of the line is less than the dash+gap length, at which point the remainder is drawn as-is with the input pen.
If you wanted to implement Path drawing, then you would have to interpolate around the intermediate vertices (unless you wanted to cheap out and just compute dashes for each segment indepdently).
Here is how you would call it:
I don't claim that the code is great (or even necessarily good ;-), but it should give you a good starting point if you are really interested in drawing your directed line.
Good luck!
嗯...也许我遗漏了一些东西,但是当我使用这个(几乎)完全相同的代码时,我得到一个只有一个点的定向箭头。
这是我的代码:
和输出:
Hmm... maybe I am missing something, but when I use this (almost) exact same code, I get a directed arrow with only one point.
Here is my code:
And the output: