private void AddFigureToCanvas(Figure singleFigure)
        {
            Shape figure = null;
            if (singleFigure.Fig == FigureType.Line) // line
            {
                figure = new Line();
                (figure as Line).X1 = singleFigure.X1;
                (figure as Line).Y1 = singleFigure.Y1;
                (figure as Line).X2 = singleFigure.X2;
                (figure as Line).Y2 = singleFigure.Y2;
            }
            else
            {
                if (singleFigure.Fig == FigureType.Ellipse) // ellipse
                {
                    figure = new Ellipse();
                }
                else if (singleFigure.Fig == FigureType.Rectangle) // rectangle
                {
                    figure = new Rectangle();
                }
                else
                {
                    return;
                }

                figure.Height = Math.Abs(singleFigure.Y2 - singleFigure.Y1);
                figure.Width = Math.Abs(singleFigure.X2 - singleFigure.X1);
                Windows.UI.Xaml.Controls.Canvas.SetTop(figure, Math.Min(singleFigure.Y1, singleFigure.Y2));
                Windows.UI.Xaml.Controls.Canvas.SetLeft(figure,Math.Min(singleFigure.X1, singleFigure.X2));
            }

            figure.StrokeThickness = 10;
            figure.Fill = new SolidColorBrush(singleFigure.Color);
            figure.Stroke = figure.Fill;
            Canvas.Children.Add(figure);
        }
        private void Canvas_PointerReleased(object sender, PointerRoutedEventArgs e)
        {
            if (figureStartPoint != null)
            {
                var index = (int)((Figures.Margin.Left - 10) / 70);
                Figure figure = new Figure();
                figure.X1 = figureStartPoint.Position.X;
                figure.Y1 = figureStartPoint.Position.Y;
                figure.X2 = e.GetCurrentPoint(Canvas).Position.X;
                figure.Y2 = e.GetCurrentPoint(Canvas).Position.Y;

                figure.Fig = (FigureType)index;

                switch ((int)rotateColors.Angle)
                {
                    case 0:
                        figure.Color = Windows.UI.Color.FromArgb(255, 255, 0, 0);
                        break;
                    case 90:
                        figure.Color = Windows.UI.Color.FromArgb(255, 0, 255, 0);
                        break;
                    case 180:
                        figure.Color = Windows.UI.Color.FromArgb(255, 0, 0, 255);
                        break;
                    case 270:
                        figure.Color = Windows.UI.Color.FromArgb(255, 0, 0, 0);
                        break;
                    default:
                        break;
                }

                AddFigureToCanvas(figure);
                figureStartPoint = null;
            }
        }
        private string SerializeCanvas()
        {
            var shapes = new List<Figure>();

            foreach (var item in Canvas.Children)
            {
                var shape = item as Shape;
                if (shape != null)
                {
                    var newShape = new Figure();
                    var line = shape as Line;
                    if (line != null)
                    {
                        newShape.Fig = FigureType.Line;
                        newShape.X1 = line.X1;
                        newShape.Y1 = line.Y1;
                        newShape.X2 = line.X2;
                        newShape.Y2 = line.Y2;
                    }
                    else
                    {
                        newShape.Fig = ((shape as Rectangle) != null) ? FigureType.Rectangle : FigureType.Ellipse;
                        newShape.X1 = Windows.UI.Xaml.Controls.Canvas.GetTop(shape);
                        newShape.Y1 = Windows.UI.Xaml.Controls.Canvas.GetLeft(shape);
                        newShape.X2 = newShape.X1 + shape.Width;
                        newShape.Y2 = newShape.Y1 + shape.Height;                        
                    }

                    newShape.Color = (shape.Fill as SolidColorBrush).Color;
                    shapes.Add(newShape);
                }
            }

            var result = JsonConvert.SerializeObject(shapes);
            return result;
        }