Esempio n. 1
0
        public static CanvasModel ReadFromFile(string fileName)
        {
            CanvasModel model = new CanvasModel();

            string[] content = File.ReadAllLines(fileName);
            model.CurrentImagePath = content[0];
            try
            {
                model.LoadImage(new BitmapImage(new Uri(content[0])));
            }
            catch (FileNotFoundException e)
            {
                throw new BindedImageFileNotFoundException(content[0]);
            }

            for (int i = 1; i < content.Length; i++)
            {
                string[] tempArr = content[i].Split(';');
                Shape    s       = null;
                Point    p       = new Point();

                if (tempArr[0] == "0")
                {
                    s        = new Ellipse();
                    p        = new Point(double.Parse(tempArr[1]), double.Parse(tempArr[2]));
                    s.Width  = double.Parse(tempArr[3]);
                    s.Height = double.Parse(tempArr[4]);
                }
                else if (tempArr[0] == "1")
                {
                    s        = new Rectangle();
                    p        = new Point(double.Parse(tempArr[1]), double.Parse(tempArr[2]));
                    s.Width  = double.Parse(tempArr[3]);
                    s.Height = double.Parse(tempArr[4]);
                }
                else if (tempArr[0] == "2")
                {
                    s = new Polygon();
                    Polygon pol = (Polygon)s;
                    p = new Point(double.Parse(tempArr[1]), double.Parse(tempArr[2]));

                    for (int j = 3; j < tempArr.Length; j += 2)
                    {
                        if (!string.IsNullOrEmpty(tempArr[j]))
                        {
                            pol.Points.Add(new Point(double.Parse(tempArr[j]), double.Parse(tempArr[j + 1])));
                        }
                    }
                }

                if (s != null)
                {
                    model.AddShape(s, p);
                }
            }
            return(model);
        }
Esempio n. 2
0
        private void mitem_FileOpen_Click(object sender, RoutedEventArgs e)
        {
            OpenFileDialog dialog = new OpenFileDialog()
            {
                DefaultExt = ".jpg",
                Filter     = "ShapeSelector shape template file (*.shps)|*.shps|JPEG Files (*.jpg, *.jpeg)|*.jpg;*.jpeg|PNG Files (*.png)|*.png|GIF Files (*.gif)|*.gif"
            };

            bool?result = dialog.ShowDialog();

            if (result == true)
            {
                string fileName = dialog.FileName;
                string ext      = dialog.FileName.Substring(dialog.FileName.Length - 5);

                if (ext == ".shps")
                {
                    try
                    {
                        canvasModel   = FileManager.ReadFromFile(fileName);
                        canvas.Width  = canvasModel.CurrentImage.Width;
                        canvas.Height = canvasModel.CurrentImage.Height;
                        currentMode.UpdateModel(canvasModel);
                    }
                    catch (BindedImageFileNotFoundException ex)
                    {
                        MessageBox.Show("Nie odnaleziono powiązanego pliku z obrazem: " + ex.FileName,
                                        "Nie znaleziono obrazu",
                                        MessageBoxButton.OK,
                                        MessageBoxImage.Error);
                        return;
                    }
                }
                else
                {
                    var img = new BitmapImage(new Uri(dialog.FileName));
                    canvasModel.CurrentImagePath = dialog.FileName;
                    canvasModel.LoadImage(img);
                    canvasModel.ClearShapes();
                    canvas.Width  = img.Width;
                    canvas.Height = img.Height;
                }

                RefreshCanvas();
                RefreshTable();
            }
        }