Esempio n. 1
0
        /// <summary>
        /// Открытие файла и добавление элементов на поле
        /// </summary>
        private void Open_Executed(object sender, ExecutedRoutedEventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();

            ofd.Filter = "Файлы (dat)|*.dat|Все файлы|*.*";
            var result = ofd.ShowDialog();

            if (result == true)
            {
                try
                {
                    // создаем объект BinaryReader
                    using (BinaryReader reader = new BinaryReader(File.Open(ofd.FileName, FileMode.Open)))
                    {
                        shapes.Clear();
                        paint.Children.Clear();

                        // пока не достигнут конец файла
                        // считываем каждое значение из файла
                        while (reader.PeekChar() > -1)
                        {
                            double ShapePosX       = reader.ReadDouble();
                            double ShapePosY       = reader.ReadDouble();
                            int    StrokeThickness = reader.ReadInt32();
                            double OuterRadius     = reader.ReadDouble();
                            double InnerRadius     = reader.ReadDouble();
                            byte   BR = reader.ReadByte();
                            byte   BG = reader.ReadByte();
                            byte   BB = reader.ReadByte();
                            byte   BA = reader.ReadByte();

                            byte SR = reader.ReadByte();
                            byte SG = reader.ReadByte();
                            byte SB = reader.ReadByte();
                            byte SA = reader.ReadByte();



                            Shape s = new Shape
                            {
                                ShapePositionX  = ShapePosX,
                                ShapePositionY  = ShapePosY,
                                StrokeThickness = StrokeThickness,
                                OuterRadius     = OuterRadius,
                                InnerRadius     = InnerRadius,
                                RColor          = BR,
                                GColor          = BG,
                                BColor          = BB,
                                AColor          = BA,

                                SRColor = SR,
                                SGColor = SG,
                                SBColor = SB,
                                SAColor = SA
                            };

                            s.InitShape(paint);
                            shapes.Add(s);
                        }
                    }

                    this.Title = "Фигуры - " + ofd.FileName;
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }
        }