I have defined the class Intersection as:
// ******************************************** class Intersection
public class Intersection
{
public string name { get; set; }
public Color fill { get; set; }
public bool present { get; set; }
public Region region { get; set; }
public GraphicsPath thick_path { get; set; }
public GraphicsPath thin_path { get; set; }
public Intersection ( string Name,
Color Fill,
GraphicsPath Thick_path )
{
name = Name;
fill = Fill;
present = true;
region = null;
thick_path = new GraphicsPath ( );
thick_path = Thick_path;
}
} // class Intersection
and I declare a List of Intersections as:
List < Intersection > intersections =
new List < Intersection > ( );
In my application, I add elements to the List with the following method:
// ********************************* create_intersection_paths
// https://stackoverflow.com/questions/11553278/
// graphics path-using-rotate-at-drawing-with-gdi
// p0 thru p9 define the north lanes; the east, south, and
// west lanes are rotations and translation of the north lanes
//
// there are two sets of rotation and translation: the first
// for the roadway and median boundaries (thick_path); the
// second for the lane boundaries (thin_path)
//
// note that only the outbound lanes are drawn
void create_intersection_paths ( )
{
float angle = 90.0f;
Intersection intersection;
GraphicsPath thick_path = new GraphicsPath ( );
GraphicsPath thin_path = new GraphicsPath ( );
Matrix transform_matrix = new Matrix ( );
PointF p0 = new PointF ( left_side, top );
PointF p1 = new PointF ( left_side, bottom );
PointF p2 = new PointF ( left_side + w, top );
PointF p3 = new PointF ( left_side + w, bottom );
PointF p4 = new PointF ( left_side + w2, top );
PointF p5 = new PointF ( left_side + w2, bottom );
PointF p6 = new PointF ( graphic_center.X, top );
PointF p7 = new PointF ( graphic_center.X, bottom );
PointF p8 = new PointF ( right_side, top );
PointF p9 = new PointF ( right_side, bottom );
thick_path.StartFigure ( );
thick_path.AddLine ( p0, p1 );
thick_path.StartFigure ( );
thick_path.AddLine ( p6, p7 );
thick_path.StartFigure ( );
thick_path.AddLine ( p8, p9 );
for ( int i = 0; ( i < NUMBER_INTERSECTIONS ); i++ )
{
transform_matrix.RotateAt ( angle,
graphic_center,
MatrixOrder.Prepend );
thick_path.Transform ( transform_matrix );
intersection = new Intersection (
intersection_index2name [ i ],
SystemColors.Window,
thick_path );
intersections.Add ( intersection );
}
transform_matrix.Reset ( );
thin_path.StartFigure ( );
thin_path.AddLine ( p2, p3 );
thin_path.StartFigure ( );
thin_path.AddLine ( p4, p5 );
for ( int i = 0; ( i < NUMBER_INTERSECTIONS ); i++ )
{
transform_matrix.RotateAt ( angle,
graphic_center,
MatrixOrder.Prepend );
thin_path.Transform ( transform_matrix );
intersections [ i ].thin_path = thin_path;
}
thick_path.Dispose ( );
thin_path.Dispose ( );
transform_matrix.Dispose ( );
} // create_intersection_paths
When debugging within this method, the thick_paths and thin_paths in each of the instances of intersection are valid. The intersections are painted on the monitor with the following method:
// ****************************** draw_intersection_background
void draw_intersection_background ( Graphics graphics )
{
Pen thick_pen = new Pen ( Color.Black, 4.0f );
Pen thin_pen = new Pen ( Color.Black, 1.0f );
foreach ( Intersection intersection in intersections )
{
if ( intersection.present )
{
graphics.DrawPath ( thick_pen,
intersection.thick_path );
graphics.DrawPath ( thin_pen,
intersection.thin_path );
}
}
} // draw_intersection_background
However, the statement (at line 465)
graphics.DrawPath ( thick_pen,
intersection.thick_path );
causes an exception:
System.ArgumentException was unhandled
Message="Parameter is not valid."
Source="System.Drawing"
StackTrace:
at System.Drawing.Graphics.CheckErrorStatus(Int32 status)
at System.Drawing.Graphics.DrawPath(Pen pen, GraphicsPath path)
at DefineIntersection.Define_Intersection.draw_intersection_background(Graphics graphics) in
C:\Gustafson\Traffic_Engineering\Synchronization\DefineIntersection\Define_Intersection.cs:line 465
at DefineIntersection.Define_Intersection.intersection_PB_Paint(Object sender, PaintEventArgs e) in
C:\Gustafson\Traffic_Engineering\Synchronization\DefineIntersection\Define_Intersection.cs:line 492
at System.Windows.Forms.PictureBox.OnPaint(PaintEventArgs pe)
at System.Windows.Forms.Control.PaintWithErrorHandling(PaintEventArgs e, Int16 layer)
at System.Windows.Forms.Control.WmPaint(Message& m)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
InnerException:
intersection.thick_path is invalid.
Thoughts?
Environment:
Windows 7 Professional (x64) Service Pack 1 (build 7601)
Microsoft Visual Studio 2008 Professional Edition
Microsoft Visual C# 2008
Microsoft .NET Framework 3.5 SP1
Because thick_path is disposed.
This is weird:
thick_path = new GraphicsPath ( );
thick_path = Thick_path;
I guess that what you actually want to do is this:
thick_path = (GraphicsPath)Thick_path.Clone();
And BTW, in draw_intersection_background you forget to dispose your pens. It should be:
using Pen thick_pen = new Pen ( Color.Black, 4.0f );
using Pen thin_pen = new Pen ( Color.Black, 1.0f );