Title: Save and restore line drawings in C#
The example Draw and move line segments in C# shows how to build a simple line editor. This example adds the ability to save and restore drawings.
The first step is to save all of the drawing information in a single object. You can save the information on an array or list, but it's probably easiest if you save everything in an object from a class that you define.
The previous version of the program stored the end points of the line segments in two lists of points. This example uses the following class to represent a drawing.
// A class to serialize and deserialize drawings.
public class Drawing
{
public List<Point> Pt1 = new List<Point>();
public List<Point> Pt2 = new List<Point>();
}
The main program uses the following object to store the lines' points.
// The points that make up the line segments.
private Drawing TheDrawing = new Drawing();
Most of the main program's code is similar to the previous version except it uses this object instead of separate point lists.
The following code shows how the program saves a drawing.
// Save a serialization of the drawing.
private void mnuFileSaveAs_Click(object sender, EventArgs e)
{
if (sfdSave.ShowDialog() == DialogResult.OK)
{
// Serialize.
XmlSerializer xml_serializer =
new XmlSerializer(TheDrawing.GetType());
using (StreamWriter stream_writer =
new StreamWriter(sfdSave.FileName))
{
xml_serializer.Serialize(stream_writer, TheDrawing);
stream_writer.Close();
}
}
}
This code displays a SaveFileDialog. If the user selects a file and clicks Save, the code create an XmlSerializer to work with the type of the object TheDrawing. It creates a StreamWriter to write into the desired file and then uses the serializer to write the drawing object's serialization into the file.
The following code shows how the program loads a saved drawing.
// Load a serialization of a drawing.
private void mnuFileOpen_Click(object sender, EventArgs e)
{
if (ofdLoad.ShowDialog() == DialogResult.OK)
{
try
{
XmlSerializer xml_serializer =
new XmlSerializer(TheDrawing.GetType());
using (FileStream file_stream =
new FileStream(ofdLoad.FileName, FileMode.Open))
{
Drawing new_drawing =
(Drawing)xml_serializer.Deserialize(file_stream);
TheDrawing = new_drawing;
picCanvas.Refresh();
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
This code displays a OpenFileDialog. If the user selects a file and clicks Open, the code create an XmlSerializer to work with the type of the object TheDrawing. It creates a StreamWriter to read from the selected file and then uses the serializer to deserialize the data in the file, converting the result into a Drawing object. If all of that succeeds, the code saves the new Drawing object in the variable TheDrawing and redraws.
The code works inside a try catch block in case something goes wrong. For example, if the user selects a file that doesn't hold a line drawing serialization or if the file is corrupted, then the code does not replace the previous drawing.
Download the example to experiment with it and to see additional details.
|