来自 .NET 的 CustomLineCap 构造函数的 NotImplementedException
我想画一个自定义线帽 - 一个半径为 r 的等边三角形。显然我不能:
Dim triangleSide As Single = CSng(3 * r / Math.Sqrt(3))
Dim triangleHeight As Single = CSng(3 * r / 2)
path = New GraphicsPath()
Dim points() As PointF = New PointF() { _
New PointF(-triangleSide / 2, 0), _
New PointF(triangleSide / 2, 0), _
New PointF(0, triangleHeight) }
path.AddLines(points)
' Not Implemented Exception, Was is Das? '
_HlpCap = New CustomLineCap(path, Nothing)
是我做错了什么还是这只是一个框架错误?
编辑:
在 Mark Cidade 评论之后,我尝试使用 (Nothing, path)
并且它有所帮助,但我需要填充三角形,而不仅仅是将其描边......
I want to draw a custom line cap - a equilateral triangle with the radius r. Apparently I can't:
Dim triangleSide As Single = CSng(3 * r / Math.Sqrt(3))
Dim triangleHeight As Single = CSng(3 * r / 2)
path = New GraphicsPath()
Dim points() As PointF = New PointF() { _
New PointF(-triangleSide / 2, 0), _
New PointF(triangleSide / 2, 0), _
New PointF(0, triangleHeight) }
path.AddLines(points)
' Not Implemented Exception, Was is Das? '
_HlpCap = New CustomLineCap(path, Nothing)
Do I something wrong or it's just a framework bug?
EDIT:
After Mark Cidade remark, I tried using (Nothing, path)
and it helped, but I need to fill in the triangle, not only to stroke it out...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
该异常来自 GDI+ 库从其
GdipCreateCustomLineCap()
函数返回NotImplemented
状态。尝试传递笔划路径而不是Nothing
:The exception comes from the GDI+ library returning a
NotImplemented
status from itsGdipCreateCustomLineCap()
function. Try passing a stroke path instead ofNothing
:显然,路径不能穿过 x 轴。我使用此代码创建了箭头帽:
Apparently, the path cannot cross the x-axis. I used this code to create an arrow cap:
错误
下面是一个
GraphicsPath
的示例,它尝试在行尾之外绘制箭头并导致System.NotImplementedException
。此操作会失败,因为路径必须与负 Y 轴相交。但上面的路径只经过原点,实际上并没有到达负 Y 轴。
中的注释这些文档至关重要:
这是措辞不佳的情况之一。文档说“应该拦截”,但我认为它“必须拦截”,否则你会得到一个
System.NotImplementedException.此拦截问题仅适用于
fillPath
,而不适用于strokePath
。 (文档也可能使用一些图片。)好
这是一个
GraphicsPath
的示例,它在线的末尾绘制一个箭头并且工作正常。无论如何,这可能是大多数人想要绘制的箭头。修复您的示例
修复方法是从 y 中减去
triangleHeight
。这会将箭头的尖端放置在 0,0(这是线末端的坐标),这可能正是您想要的,并将三角形的底边放置在-triangleHeight
处。为您提供的确切修复(在 Visual Basic 中):
Bad
Here's an example of a
GraphicsPath
that tries to draw an arrow head beyond the end of the line and causes aSystem.NotImplementedException
.This fails because the path must intercept the negative Y-axis. But the path above only passes through the origin and doesn't actually hit the negative Y-axis.
The remarks in the docs are crucially important:
This is one of those cases where the wording is poor. The docs say "should intercept" but I think it's the case that it "must intercept" or else you're going to get a
System.NotImplementedException
. This intercept issue applies only to afillPath
, and not astrokePath
. (The documentation could probably use some pictures too.)Good
Here's an example of a
GraphicsPath
that draws an arrow head at the end of the line and works correctly. And it's likely the kind of arrow head that most people want to draw anyway.Fixing Your Example
A fix is to just subtract the
triangleHeight
from y. This places the tip of the arrow at 0,0 (which are the coordinates of the end of the line) which is likely what you intended anyway, and puts the base of the triangle at-triangleHeight
.The exact fix for you (in Visual Basic):
就我而言,我根据容器的宽度设置了盖子各段的长度。每当我将表单缩小到超过一定限制时,我的程序总是会因异常而停止。
然后我发现当任何段的长度达到1时就会抛出异常。
因此,请确保 cap 路径中的所有段的长度都大于 1。
In my case, I had made the lenght of the cap's segments dependant on the conteiner's width. My program always stopped with an exception whenever I shrinked the form past a certain limit.
Then I discovered that the exception was thrown when any segment's length reached 1.
So, make sure that all the segments in your cap path have a length greater than 1.