BLOG.CSHARPHELPER.COM: Let the user draw curves and scribble on a PictureBox in C#
Let the user draw curves and scribble on a PictureBox in C#
The Graphics class provides a DrawLines method that draws a series of connected lines, also called a polyline. This program lets the user create a series of polylines.
The program stores the points that make up a polyline as a List<Point>. It stores a series of polylines in a List<List<Point>>. It keeps track of the new polyline that the user is drawing in the variable NewPolyline.
// The polylines we draw. private List<List<Point>> Polylines = new List<List<Point>>();
// The new polyline we are drawing. private List<Point> NewPolyline = null;
When the user presses the mouse down, the following code creates a new polyline.
// Start drawing. private void picCanvas_MouseDown(object sender, MouseEventArgs e) { // Create the new polyline. NewPolyline = new List<Point>(); Polylines.Add(NewPolyline);
// Add the first point. NewPolyline.Add(e.Location); }
When the user moves the mouse, the following code adds the mouse's new location to the new polyline. Notice how the code checks that the user is making a new polyline by checking whether NewPolyline == null.
Comments