我有完善的代码可以从文本文件加载和保存形状,该文本文件包含形状的类型(我使用称为shapetype的枚举),x1,y1(鼠标向下的初始位置)坐标和x2,y2(将鼠标拖到某处并释放左键后,鼠标光标的最终位置)。数据还将形状的颜色存储为rgb int值,以及将形状填充为实心还是仅填充轮廓时的true / false值。
我要问的唯一问题是我怎么能拥有完全相同的东西,但 在文本文件中一行上的1个形状的所有数据并且仍然能够加载它。我知道如何将数据保存在一行上,但是如果每种形状的所有内容都在一行上,我不知道如何加载数据...
现在的代码是:
x
具有包含形状数据的文本的文件的外观是: Current file format
答案 0 :(得分:0)
最终结果
private void saveCanvas()
{
SaveFileDialog save = new SaveFileDialog();
save.DefaultExt = ".shapes";
save.Filter = "Shapes File (*.shapes) |*.shapes";
if (save.ShowDialog() == DialogResult.OK)
{
File.Delete(save.FileName);
var settings = new JsonSerializerSettings { TypeNameHandling = TypeNameHandling.Objects, NullValueHandling = NullValueHandling.Ignore };
File.WriteAllText(save.FileName, JsonConvert.SerializeObject(shapes, shapes.GetType(), settings));
}
}
private void loadCanvas()
{
List<APShape> shapeList = new List<APShape>();
OpenFileDialog open = new OpenFileDialog();
open.Filter = "Shapes File (*.shapes) |*.shapes";
if (open.ShowDialog() == DialogResult.OK)
{
shapes.Clear(); /* Clear canvas to make sure we are starting blank */
canvas.Invalidate();
var json = File.ReadAllText(open.FileName);
var settings = new JsonSerializerSettings { TypeNameHandling = TypeNameHandling.Objects, NullValueHandling = NullValueHandling.Ignore };
shapeList = JsonConvert.DeserializeObject<List<APShape>>(json, settings);
foreach (var shape in shapeList)
{
shapes.Add(shape);
}
canvas.Invalidate();
}
}