Search code examples
c#listgenericsdrawing2dgraphicspath

Is List<GraphicsPath> possible in C#?


I'm trying to use a list of GraphicsPath instead of an array as I don't know the number of the paths that will be created by user.

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

After this I fill the List as follows:

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

But when I try to use the GraphicsPath from the list, while the Count property is correct, I cannot use the object like below because of an argument exception.

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)
      }
   }

Is it something related to GraphicsPath being Disposabe ?? Or doing smt wrong?


Solution

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

    This is a local graphics path. Your using block Disposes it after the scope if done. So you need to remove that using block and instead dispose your paths when you no longer need them.