2017-12-18 155 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塊,而是當你不再需要它們時,處置你的路徑。

相關問題