Reputation: 189
I need assistance with saving drawn shapes on a winform using graphics from a list I created that store the shape type and the information for the shape the user draws, into a text file that encodes it into UTF8 (or to their choosing).
My problem is after saving the "drawing", which has shape type lines, rectangle, etc. as a text file. How do I open it in the winform application and parse the numbers and re-create the shape on the winform using graphics and paint event?
What I currently have in code, is the user draws shapes onto a graphic handled by mouse events, and then it goes into a list. The user also selects the encoding type for the text file (like Encoding.ASCII, etc.) Then I have a save menu where the user saves the file into a location. There is also a clear button to clear the drawings and such. Then I can't figure out how to open the text file to re-create that saved drawing that was cleared.
I looked at the msdn API about creating my own format for the encoding, but I'm confused on how to do it.
What I thought about doing was checking what encoding the user saved it, and then choosing the appropriate custom format to re-create the shape on the winform graphic.
Any ideas? Thank you.
EDIT: The code I'm using for saving the text file:
using (BinaryWriter save_bin = new BinaryWriter(File.Open(save_fd.FileName, FileMode.OpenOrCreate), Encoding.UTF8))
{
foreach (Shape a in shapeList)
{
save_bin.Write(a.ToString());
}
save_bin.Close();
}
I think I should not use ToString(), but something else to write.
Upvotes: 0
Views: 964
Reputation: 367
More info on serialization:
Firstly, your classes should contain the [Serializable] attribute
[Serializable]
public class Example()
{
}
Then you have the ability to save it like this:
Example obj = new Example();
// Set properties of the 'Example' object
IFormatter formatter = new BinaryFormatter(); // Save the file
Stream stream = new FileStream("MyFile.bin", FileMode.Create, FileAccess.Write, FileShare.None);
formatter.Serialize(stream, obj);
stream.Close();
Restoring the file back to your class can be done like this:
IFormatter formatter = new BinaryFormatter();
Stream stream = new FileStream("MyFile.bin", FileMode.Open, FileAccess.Read, FileShare.Read);
MyObject obj = (MyObject) formatter.Deserialize(stream);
stream.Close();
Source: https://msdn.microsoft.com/en-us/library/4abbf6k0(v=vs.110).aspx
Upvotes: 1
Reputation: 15294
Do this, from NuGet, download JSON.Net
PM> Install-Package Newtonsoft.Json
Next, copy this code
string jsonStateOfShape = JsonConvert.SerializeObject(shapeList);
using (FileStream stream = new FileStream("shapes.json", FileMode.Create))
using (StreamWriter writer = new StreamWriter(stream))
{
writer.Write(jsonStateOfShape);
}
// To re open
string reopenedState = string.Empty;
using (FileStream stream = new FileStream("shapes.json", FileMode.Open))
using (StreamReader reader = new StreamReader(stream))
{
reopenedState = reader.ReadToEnd();
}
var shapes = JsonConvert.DeserializeObject<List<Shape>>(reopenedState);
shapes.ForEach(shapeList.Add);
// dont try to replace shapeList directly, referencing issues...
Upvotes: 1