【问题标题】:Is List<GraphicsPath> possible in C#?List<GraphicsPath> 可以在 C# 中使用吗?
【发布时间】:2018-05-31 14:48:06
【问题描述】:

我正在尝试使用 GraphicsPath 列表而不是数组,因为我不知道用户将创建的路径数。

List<GraphicsPath> PathDB = new List<GraphicsPath>();

在此之后,我填写列表如下:

using(GraphicsPath myPath = new GraphicsPath())
{
   myPath.AddPolygon(myPoints);
   PathDB.Add(myPath);
}

但是当我尝试使用列表中的 GraphicsPath 时,虽然 Count 属性是正确的,但由于参数异常,我无法使用如下所示的对象。

num = PathDB.Count;
for(int k=0; k < num; k++)
   {
      using(GraphicsPath myCurrentPath = new GraphicsPath())
      {
         myCurrentPath = PathDB[k];
         myCurrentPath.AddLine(0,0,400,400); //at this stage exception is thrown 
         myGraphics.DrawPath(myPen, myCurrentPath)
      }
   }

这是否与 GraphicsPath 被 Disposabe 相关?还是smt做错了?

【问题讨论】:

  • 异常说明了什么?是的,该对象已被处置,因此您不应再使用它。你想用它来达到什么目的?
  • 那个 using 声明毫无意义。您当然不能破坏存储在该列表中的任何内容,当它从列表中删除时必须这样做。 C# 足够聪明,不会造成严重破坏,但您可能也在其他地方这样做。
  • 我不明白。为什么要在循环中创建GraphicsPath 的新实例,用现有值(您之前已处置)覆盖变量,然后丢弃新创建的值而不使用它?您似乎不清楚 using 实际做了什么(或者您的代码为此做了什么)。

标签: c# list generics drawing2d graphicspath


【解决方案1】:
using(GraphicsPath myPath = new GraphicsPath())
{
   myPath.AddPolygon(myPoints);
   PathDB.Add(myPath);
} // this disposes myPath

这是一个本地图形路径。如果完成,您的using 会在范围之后阻止Disposes。因此,您需要删除 using 块,并在不再需要路径时处理它们。

【讨论】:

    猜你喜欢
    • 2011-05-30
    • 2010-09-29
    • 1970-01-01
    • 2012-07-09
    • 2012-07-02
    • 1970-01-01
    • 2012-04-11
    • 1970-01-01
    • 2020-08-26
    相关资源
    最近更新 更多