2017-12-18 378 views
-1

我想使用GraphicsPath而不是数组的列表,因为我不知道将由用户创建的路径的数量。C#中List <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相关?或者做错了吗?

+1

这个例外说什么?是的,物体被丢弃,所以你不应该再使用它了。你想用它达到什么目的? –

+1

*使用*语句没有任何意义。当然,您不得销毁存储在该列表中的任何内容,该内容必须在从列表中删除*时完成。 C#足够聪明,不会造成严重破坏,但是你可能在其他地方也这样做。 –

+0

我不明白。为什么你要在你的循环中创建一个'GraphicsPath'的新实例,用一个已经存在的值(你之前已经处理过)覆盖变量,然后丢弃新创建的值而不使用它?看起来你并不清楚'使用'实际上做了什么(或者你的代码在这方面做了什么)。 – Sefe

回答

3
using(GraphicsPath myPath = new GraphicsPath()) 
{ 
    myPath.AddPolygon(myPoints); 
    PathDB.Add(myPath); 
} // this disposes myPath 

这是一个本地图形路径。您的using区块Dispose在完成范围之后。所以你需要删除using块,而是当你不再需要它们时,处置你的路径。

相关问题