首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >GraphicsPath和OutOfMemoryException

GraphicsPath和OutOfMemoryException
EN

Stack Overflow用户
提问于 2011-08-03 22:06:21
回答 4查看 1.1K关注 0票数 3

我有以下几点

代码语言:javascript
复制
private bool IsPathVisible(Rectangle detectorRectangle, GraphicsPath path, Pen pen)
{
    path.Widen(pen);
    return IsPathVisible(detectorRectangle, path);
}

path点是同一个点时,我收到一个OutOfMemoryException (使用Widen函数)。

我该如何管理它?

EN

回答 4

Stack Overflow用户

发布于 2011-08-03 22:10:51

这是画笔和widen方法的错误。请确保路径的起点和终点不同。

这是一个演示:

代码语言:javascript
复制
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

票数 6
EN

Stack Overflow用户

发布于 2016-11-18 13:19:52

我也一直在为这个例外而痛苦。建议如下:

  1. 在加宽之前保存点,以查看导致OutOfMemoryException的确切点:

私有布尔点(Rectangle detectorRectangle,GraphicsPath path,画笔){ var IsPathVisible= path.PathPoints.Clone() as PointF[];path.Widen(笔);return IsPathVisible(detectorRectangle,path);}

您可能会看到,可能存在具有相同坐标的后续点。实际上是他们造成了这个问题。

  1. 另外,GraphicsPath可以由多个子路径组成。为了进行可靠的命中测试,我推荐以下几点:

公共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来查找是否命中其中任何一个区域

票数 1
EN

Stack Overflow用户

发布于 2011-08-03 23:08:30

如果路径为IsPoint,则不要加宽。

代码语言:javascript
复制
<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 
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/6927774

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档