我有以下几点
private bool IsPathVisible(Rectangle detectorRectangle, GraphicsPath path, Pen pen)
{
path.Widen(pen);
return IsPathVisible(detectorRectangle, path);
}当path点是同一个点时,我收到一个OutOfMemoryException (使用Widen函数)。
我该如何管理它?
发布于 2011-08-03 22:10:51
这是画笔和widen方法的错误。请确保路径的起点和终点不同。
这是一个演示:
private void panel1_Paint(object sender, PaintEventArgs e)
{
//This works:
using (GraphicsPath path = new GraphicsPath())
{
path.AddLine(new Point(16, 16), new Point(20, 20));
path.Widen(Pens.Black);
e.Graphics.DrawPath(Pens.Black, path);
}
//This does not:
using (GraphicsPath path = new GraphicsPath())
{
path.AddLine(new Point(20, 20), new Point(20, 20));
path.Widen(Pens.Black);
e.Graphics.DrawPath(Pens.Black, path);
}
}这就是报告给微软的地方:GraphicsPath.Widen throw OutOfMemoryException if the path has a single point
发布于 2016-11-18 13:19:52
我也一直在为这个例外而痛苦。建议如下:
私有布尔点(Rectangle detectorRectangle,GraphicsPath path,画笔){ var IsPathVisible= path.PathPoints.Clone() as PointF[];path.Widen(笔);return IsPathVisible(detectorRectangle,path);}
您可能会看到,可能存在具有相同坐标的后续点。实际上是他们造成了这个问题。
公共Region[] CreateRegionFromGraphicsPath(GraphicsPath路径,笔wideningPen) { var regions =新列表();var itPath =新列表(路径);itPath.Rewind();var curSubPath =新GraphicsPath();for (int i= 0;i< itPath.SubpathCount;i++) { bool isClosed;itPath.NextSubpath(curSubPath,out isClosed);if (!isClosed && CanWiden(curSubPath)) curSubPath.Widen();//加宽未闭合路径int regionIndex =I/ 100;//最大区域扫描矩形计数if (regions.Count < regionIndex + 1) {regions.Add(新区域(CurSubPath));} else { regionsregionIndex.Union(curSubPath);}} curSubPath.Dispose();itPath.Dispose();return regions.ToArray();}/确定加宽此图形路径是否不会导致异常/公共静态布尔CanWiden(GraphicsPath gp) { const float regionPointsTolerance = 1e-8f;var pts = gp.PathPoints;if (pts.Length < 2) return false;for (int i= 1;i< pts.Length;i++) { if (Math.Abs(ptsi-1.X - ptsi.X) < regionPointsTolerance && Math.Abs(ptsi-1.Y - ptsi.Y) < regionPointsTolerance) return false;} return true;}
然后,您只需调用区域的IsVisible来查找是否命中其中任何一个区域
发布于 2011-08-03 23:08:30
如果路径为IsPoint,则不要加宽。
<System.Runtime.CompilerServices.Extension()> _
Public Function IsPoint(ByVal path As System.Drawing.Drawing2D.GraphicsPath) As Boolean
If path Is Nothing Then Throw New ArgumentNullException("path")
If path.PathPoints.Count < 2 Then Return True
If path.PathPoints(0) <> path.PathPoints(path.PathPoints.Count - 1) Then Return False
For i = 1 To path.PathPoints.Count - 1
If path.PathPoints(i - 1) <> path.PathPoints(i) Then Return False
Next i
' if all the points are the same
Return True
End Function https://stackoverflow.com/questions/6927774
复制相似问题