Пример #1
0
        private void openToolStripMenuItem_Click(object sender, EventArgs e)
        {
            DialogResult result;

            using (OpenFileDialog fileChooser = new OpenFileDialog())
            {
                fileChooser.Filter          = "Text Files (*.txt) |*.txt| Binary Files (*.bin) |*.bin";
                fileChooser.DefaultExt      = "txt";
                fileChooser.CheckFileExists = false; //Don't need to verify file name after save.
                result      = fileChooser.ShowDialog();
                currentFile = fileChooser.FileName;
                extension   = Path.GetExtension(fileChooser.FileName);
            }
            try
            {
                if (result == DialogResult.OK && extension == ".txt")
                {
                    shapeList.Clear();
                    Invalidate();
                    FileStream   fs = new FileStream(currentFile, FileMode.Open, FileAccess.Read);
                    StreamReader sr = new StreamReader(fs);


                    // Reading of Text Data
                    while (true)
                    {
                        string dataLine = sr.ReadLine();
                        if (dataLine != null)
                        {
                            dataArray = dataLine.Split(',', ';', ':'); // Splits at every , ; :

                            if (dataArray[0] == "Line")
                            {
                                Line Line = new Line(this);
                                Line.readText(sr);
                                penWidth = Convert.ToInt32(dataArray[5]);
                                penColor = Color.FromArgb(int.Parse(dataArray[6], System.Globalization.NumberStyles.AllowHexSpecifier));//string hex converted to int
                                Pen myPen = new Pen(penColor, penWidth);
                                Line = new Line(Line.Pt1, Line.Pt2, myPen);

                                shapeList.Add(Line);
                            }
                            else if (dataArray[0] == "Rectangle")
                            {
                                Rect myRect = new Rect();
                                myRect.readText(sr);
                                penWidth = Convert.ToInt32(dataArray[5]);
                                penColor = Color.FromArgb(int.Parse(dataArray[6], System.Globalization.NumberStyles.AllowHexSpecifier));
                                Pen myPen = new Pen(penColor, penWidth);
                                myRect = new Rect(myRect.Pt1, myRect.Pt2, myPen);

                                shapeList.Add(myRect);
                            }
                            else if (dataArray[0] == "FreeLine")
                            {
                                FreeLine myFreeLine = new FreeLine();
                                myFreeLine.readText(sr);
                            }
                        }
                        else
                        {
                            sr.Close();
                            Invalidate();
                            break;
                        }
                    }
                    savedFileExists = true;
                }
                else if (result == DialogResult.OK && extension == ".bin")
                {
                    string shapeType;
                    shapeList.Clear();
                    Invalidate();
                    FileStream   fs = new FileStream(currentFile, FileMode.Open, FileAccess.Read);
                    BinaryReader br = new BinaryReader(fs);

                    // Reading Binary Data
                    while (true)
                    {
                        shapeType = null;
                        if (br.BaseStream.Position != br.BaseStream.Length)
                        {
                            shapeType = br.ReadString();//Read string of the bin file
                        }
                        if (shapeType != null)
                        {
                            if (shapeType == "Line")
                            {
                                pt1.X    = br.ReadInt32();//reads 4 bytes
                                pt1.Y    = br.ReadInt32();
                                pt2.X    = br.ReadInt32();
                                pt2.Y    = br.ReadInt32();
                                penWidth = br.ReadInt32();
                                penColor = Color.FromArgb(br.ReadInt32());
                                Pen  myPen  = new Pen(penColor, penWidth);
                                Line myLine = new Line(pt1, pt2, myPen);
                                shapeList.Add(myLine);
                            }
                            else if (shapeType == "Rect")
                            {
                                pt1.X    = br.ReadInt32();
                                pt1.Y    = br.ReadInt32();
                                pt2.X    = br.ReadInt32();
                                pt2.Y    = br.ReadInt32();
                                penWidth = br.ReadInt32();
                                penColor = Color.FromArgb(br.ReadInt32());
                                Pen  myPen  = new Pen(penColor, penWidth);
                                Rect myRect = new Rect(pt1, pt2, myPen);
                                shapeList.Add(myRect);
                            }
                            else if (shapeType == "FreeLine")
                            {
                                penWidth = br.ReadInt32();
                                penColor = Color.FromArgb(br.ReadInt32());
                                Pen      myPen      = new Pen(penColor, penWidth);
                                FreeLine myFreeLine = new FreeLine(myPen);
                                int      count      = br.ReadInt32();
                                int      i          = 0;
                                while (i < count) // DONE//
                                {
                                    pt1.X = br.ReadInt32();
                                    pt1.Y = br.ReadInt32();
                                    myFreeLine.FreeList.Add(pt1);
                                    i++;
                                }
                                shapeList.Add(myFreeLine);
                            }
                        }
                        else
                        {
                            br.Close();
                            Invalidate();
                            break;
                        }
                    } //close while
                    savedFileExists = true;
                }     //close elseIf
            }
            catch (IOException)
            {
                MessageBox.Show("Error reading from file",
                                "File Error", MessageBoxButtons.OK,
                                MessageBoxIcon.Error);
            }
        }
Пример #2
0
        private void openToolStripMenuItem_Click(object sender, EventArgs e)
        {
            DialogResult result;

            using (OpenFileDialog fileChooser = new OpenFileDialog())
            {
                fileChooser.Filter          = "Text Files (*.txt) |*.txt| Binary Files (*.bin) |*.bin| Serialized Object Files (*.ser) |*.ser| XML Files (*.xml) |*.xml";
                fileChooser.DefaultExt      = "txt";
                fileChooser.CheckFileExists = false; //Don't need to verify file name after save.
                result      = fileChooser.ShowDialog();
                currentFile = fileChooser.FileName;
                extension   = Path.GetExtension(fileChooser.FileName);
            }
            try
            {
                if (result == DialogResult.OK && extension == ".txt")
                {
                    shapeList.Clear();
                    Invalidate();
                    FileStream   fs = new FileStream(currentFile, FileMode.Open, FileAccess.Read);
                    StreamReader sr = new StreamReader(fs);


                    // Reading of Text Data
                    while (true)
                    {
                        string dataLine = sr.ReadLine();
                        if (dataLine != null)
                        {
                            dataArray = dataLine.Split(',', ';', ':'); // Splits at every , ; :

                            if (dataArray[0] == "Line")
                            {
                                //Line tmpLine = new Line(this);
                                Line tmpLine = new Line();
                                tmpLine.readText(sr);
                            }
                            else if (dataArray[0] == "Rectangle")
                            {
                                Rect tmpRect = new Rect();
                                tmpRect.readText(sr);
                            }
                            else if (dataArray[0] == "FreeLine")
                            {
                                FreeLine tmpFreeLine = new FreeLine();
                                tmpFreeLine.readText(sr);
                            }
                        }
                        else
                        {
                            sr.Close();
                            Invalidate();
                            break;
                        }
                    }
                    savedFileExists = true;
                }//end text file
                else if (result == DialogResult.OK && extension == ".bin")
                {
                    string shapeType;
                    shapeList.Clear();
                    Invalidate();
                    FileStream   fs = new FileStream(currentFile, FileMode.Open, FileAccess.Read);
                    BinaryReader br = new BinaryReader(fs);

                    // Reading Binary Data
                    while (true)
                    {
                        shapeType = null;
                        if (br.BaseStream.Position != br.BaseStream.Length)
                        {
                            shapeType = br.ReadString();//Read string of the bin file
                        }
                        if (shapeType != null)
                        {
                            if (shapeType == "Line")
                            {
                                Line tmpLine = new Line();
                                tmpLine.readBinary(br);
                            }
                            else if (shapeType == "Rect")
                            {
                                Rect tmpRect = new Rect();
                                tmpRect.readBinary(br);
                            }
                            else if (shapeType == "FreeLine")
                            {
                                FreeLine tmpFreeLine = new FreeLine();
                                tmpFreeLine.readBinary(br);
                            }
                        }
                        else
                        {
                            br.Close();
                            Invalidate();
                            break;
                        }
                    } //close while
                    savedFileExists = true;
                }     //close elseIf

                //////////////////WIP/////////////////////////////////
                else if (result == DialogResult.OK && extension == ".xml")
                {
                    shapeList.Clear();
                    Invalidate();

                    XDocument menu = XDocument.Load(currentFile);
                    //
                    //
                    foreach (XElement xe in menu.Root.Elements())
                    {
                        foreach (XAttribute fe in xe.Attributes())
                        {
                            if (fe.Value == "Line")
                            {
                                // MessageBox.Show("sfsefse");
                                pt1.X    = int.Parse(xe.Element("StartPoint_X").Value);
                                pt1.Y    = int.Parse(xe.Element("StartPoint_Y").Value);
                                pt2.X    = int.Parse(xe.Element("EndPoint_X").Value);
                                pt2.Y    = int.Parse(xe.Element("EndPoint_Y").Value);
                                penWidth = int.Parse(xe.Element("PenWidth").Value);
                                penColor = Color.FromArgb(int.Parse(xe.Element("PenColor").Value, System.Globalization.NumberStyles.AllowHexSpecifier));
                                Pen  myPen  = new Pen(penColor, penWidth);
                                Line myLine = new Line(pt1, pt2, myPen);
                                shapeList.Add(myLine);
                            }
                            else if (fe.Value == "Rectangle")
                            {
                                pt1.X    = int.Parse(xe.Element("StartPoint_X").Value);
                                pt1.Y    = int.Parse(xe.Element("StartPoint_Y").Value);
                                pt2.X    = int.Parse(xe.Element("EndPoint_X").Value);
                                pt2.Y    = int.Parse(xe.Element("EndPoint_Y").Value);
                                penWidth = int.Parse(xe.Element("PenWidth").Value);
                                penColor = Color.FromArgb(int.Parse(xe.Element("PenColor").Value, System.Globalization.NumberStyles.AllowHexSpecifier));
                                Pen  myPen  = new Pen(penColor, penWidth);
                                Rect myRect = new Rect(pt1, pt2, myPen);
                                shapeList.Add(myRect);
                            }
                            else if (fe.Value == "FreeLine")
                            {
                                penWidth = int.Parse(xe.Attribute("PenWidth").Value);
                                penColor = Color.FromArgb(int.Parse(xe.Attribute("PenColor").Value, System.Globalization.NumberStyles.AllowHexSpecifier));
                                Pen      myPen      = new Pen(penColor, penWidth);
                                FreeLine myFreeLine = new FreeLine(myPen);
                                foreach (XElement point in xe.Descendants())
                                {
                                    if (point.Name == "Point") // if <Point> element
                                    {
                                        pt1.X = int.Parse(point.Attribute("X").Value);
                                        pt1.Y = int.Parse(point.Attribute("Y").Value);
                                        myFreeLine.FreeList.Add(pt1);
                                    }
                                }
                                shapeList.Add(myFreeLine);
                            }
                        }
                    }//end root element
                    savedFileExists = true;
                    Invalidate();
                }

                else if (result == DialogResult.OK && extension == ".ser")//Deserialization
                {
                    shapeList.Clear();
                    Invalidate();
                    FileStream      fs = new FileStream(currentFile, FileMode.Open, FileAccess.Read);
                    BinaryFormatter bf = new BinaryFormatter();
                    shapeList.Clear();
                    this.shapeList = (List <Shape>)bf.Deserialize(fs);

                    fs.Close();
                    Invalidate();
                }
            }//end try
            catch (IOException)
            {
                MessageBox.Show("Error reading from file",
                                "File Error", MessageBoxButtons.OK,
                                MessageBoxIcon.Error);
            }
        }