【发布时间】: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