public override void workWithFile(List <Shape> shapesList, ref DisplayManager displayManager, ref string nameWorkFile, List <System.Type> addInType) { try { DialogResult dialogResult = opendialog.ShowDialog(); nameWorkFile = Path.GetFullPath(opendialog.FileName); if (!String.Equals(opendialog.FileName, "") && dialogResult != DialogResult.Cancel && dialogResult != DialogResult.Abort) { FileStream fs = new FileStream(nameWorkFile, FileMode.OpenOrCreate); shapesList.Clear(); byte[] messageBytes = new byte[fs.Length]; fs.Read(messageBytes, 0, messageBytes.Length); string jsonString = Encoding.ASCII.GetString(messageBytes); dynamic tmpList = Newtonsoft.Json.JsonConvert.DeserializeObject <dynamic>(jsonString); List <dynamic> outputList = new List <dynamic>(); foreach (dynamic element in tmpList) { { foreach (Type type in addInType) { if (element["__type"].ToString().IndexOf(type.Name.ToString()) != -1) { outputList.Add(element); break; } } } } FileStream tmpStream = new FileStream("tmpFile.json", FileMode.Create); jsonString = JsonConvert.SerializeObject(outputList); tmpStream.Write(Encoding.ASCII.GetBytes(jsonString), 0, Encoding.ASCII.GetBytes(jsonString).Length); tmpStream.Close(); string json = File.ReadAllText("tmpFile.json"); var serializer = new DataContractJsonSerializer(typeof(List <Shape>), addInType); shapesList = (List <Shape>)serializer.ReadObject( new MemoryStream(Encoding.Unicode.GetBytes(json))); Bitmap bitmap = new Bitmap(displayManager.pictureDrawing.Width, displayManager.pictureDrawing.Height); Graphics tempGr = Graphics.FromImage(bitmap); tempGr.Clear(Color.White); WriteOnImage(tempGr, shapesList); displayManager.DeleteAll(); displayManager.InitComponent(bitmap); } } catch { MessageBox.Show("Неправильный файл", "warning", MessageBoxButtons.OK, MessageBoxIcon.Error); } }
private void pictureDrawing_MouseDown(object sender, MouseEventArgs e) { Point startPoint = new Point(e.X, e.Y); if (frame != null) { if (!frame.IsExistFrame && frame.CreateFrame(shapesList, startPoint, pictureDrawing)) { Bitmap bitmap = new Bitmap(pictureDrawing.Width, pictureDrawing.Height); Graphics tempGr = Graphics.FromImage(bitmap); tempGr.Clear(Color.White); OpenFile.WriteOnImage(tempGr, shapesList); displayManager.DeleteAll(); displayManager.InitComponent(bitmap); } else { if (frame.IsExistFrame) { frame.DeleteFrame(shapesList, pictureDrawing.Width, pictureDrawing.Height); Bitmap bitmap = new Bitmap(pictureDrawing.Width, pictureDrawing.Height); Graphics tempGr = Graphics.FromImage(bitmap); tempGr.Clear(Color.White); OpenFile.WriteOnImage(tempGr, shapesList); displayManager.DeleteAll(); displayManager.InitComponent(bitmap); frame = null; } else { frame = null; } } } else { if (numberAddTools != -1) { isMouseClick = true; shapesList.Add((Shape)Activator.CreateInstance(addinTypes[numberAddTools])); shapesList.Last().setFirstPoint(new Point(e.X, e.Y)); } } }