コード例 #1
0
ファイル: Form1.cs プロジェクト: rcreddy06/PaintApplication
        private void openMenuItem_Click(object sender, EventArgs e)
        {
            //Open Menu & check condition
            if (dataModified)
            {
                // Offer to save drawing & display dialog
                DialogResult dialogResult = MessageBox.Show("Do you want to save changes?", "Form1", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question);
                switch (dialogResult)
                {
                    case DialogResult.Yes: writeFile(currentFile);
                        break;
                    case DialogResult.No: reset();
                        this.CreateGraphics().Clear(Form1.ActiveForm.BackColor);
                        break;
                    case DialogResult.Cancel: return;
                }
            }

            //open dialog
            String line = null;
            OpenFileDialog dlg = new OpenFileDialog();
            dlg.Filter = "Text files(*.txt)|*.txt|Binary Files(*.bin)|*.bin|Serialized files(*.ser)|*.ser|XML files(*.xml)|*.xml|All files(*.*)|*.*";
            dlg.InitialDirectory = Directory.GetCurrentDirectory();

            if (dlg.ShowDialog() == DialogResult.Cancel)
                return;

            reset();
            this.CreateGraphics().Clear(Form1.ActiveForm.BackColor);

            if (dlg.ShowDialog() == DialogResult.OK)
            {
                currentFile = dlg.FileName;
                //FileStream fs = new FileStream(currentFile, FileMode.Open);
                FileInfo fileInfo = new FileInfo(dlg.FileName);
                Graphics graphics = this.CreateGraphics();

                // check for text files & selecting the shape
                if (fileInfo.Extension == ".txt")
                {
                    FileStream fs = new FileStream(currentFile, FileMode.Open);
                    StreamReader sr = new StreamReader(fs);
                    while ((line = sr.ReadLine()) != null)
                    {
                        switch (line)
                        {
                            case "Line": currentShapeType = ShapeType.Line;
                                break;
                            case "Rect": currentShapeType = ShapeType.Rectangle;
                                break;
                            case "FreeLine": currentShapeType = ShapeType.FreeLine;
                                break;
                            case "Text": currentShapeType = ShapeType.Text;
                                break;
                        }
                        shape = Shape.CreateShape(currentShapeType);

                        shape.readText(sr);
                        shape.Draw(graphics);
                        shapeList.Add(shape);
                    }
                    sr.Close();
                    fs.Close();
                }
                // Check for bin files & selecting encoding
                else if (fileInfo.Extension == ".bin")
                {
                    FileStream fs = new FileStream(currentFile, FileMode.Open);
                    BinaryReader br = new BinaryReader(fs);

                    switch (br.ReadString())
                    {
                        case "us-ascii":
                            Shape.TextEncodingType = Encoding.ASCII;
                            break;
                        case "utf-8":
                            Shape.TextEncodingType = Encoding.UTF8;
                            break;
                        case "u\0t\0f\0-\01\06\0":
                            Shape.TextEncodingType = Encoding.Unicode;
                            break;
                        case "u\0\0\0t\0\0\0f\0\0\0-\0\0\03\0\0\02\0\0\0":
                            Shape.TextEncodingType = Encoding.UTF32;
                            break;
                        case "\0u\0n\0i\0c\0o\0d\0e\0F\0F\0F\0E":
                            Shape.TextEncodingType = Encoding.BigEndianUnicode;
                            break;
                        case "utf-7":
                            Shape.TextEncodingType = Encoding.UTF7;
                            break;
                    }

                    br = new BinaryReader(fs, Shape.TextEncodingType);

                    int length = (int)br.BaseStream.Length;
                    MessageBox.Show(length.ToString());
                    while (br.PeekChar() != -1)
                    {
                        // Selecting shape for bin file & recreating the figures
                        switch (br.ReadString())
                        {
                            case "Line":
                                currentShapeType = ShapeType.Line;
                                shape = Shape.CreateShape(currentShapeType);
                                shape.readBinary(br);
                                shape.Draw(graphics);
                                shapeList.Add(shape);
                                break;

                            case "Rect":
                                currentShapeType = ShapeType.Rectangle;
                                shape = Shape.CreateShape(currentShapeType);
                                shape.readBinary(br);
                                shape.Draw(graphics);
                                shapeList.Add(shape);
                                break;

                            case "FreeLine":
                                currentShapeType = ShapeType.FreeLine;
                                shape = Shape.CreateShape(currentShapeType);
                                shape.readBinary(br);
                                shape.Draw(graphics);
                                shapeList.Add(shape);
                                break;

                            case "Text":
                                currentShapeType = ShapeType.Text;
                                shape = Shape.CreateShape(currentShapeType);
                                shape.readBinary(br);
                                shape.Draw(graphics);
                                shapeList.Add(shape);
                                break;
                        }
                    }
                    fs.Close();
                }
                else if (fileInfo.Extension == ".ser")
                {

                    FileStream fs = new FileStream(currentFile, FileMode.Open);
                    BinaryFormatter bf = new BinaryFormatter();
                    shapeList = (List<Shape>)bf.Deserialize(fs);

                    foreach (Shape shape in shapeList)

                        shape.Draw(graphics);
                    fs.Close();
                }//end ser
                else if (fileInfo.Extension == ".xml")
                {

                    XDocument document = XDocument.Load(currentFile);
                    XElement root = document.Root;
                    foreach (XElement xe in root.Elements())
                    {
                        switch (xe.Name.ToString())
                        {
                            case "Line": currentShapeType = ShapeType.Line;
                                break;
                            case "Rect": currentShapeType = ShapeType.Rectangle;
                                break;
                            case "FreeLine": currentShapeType = ShapeType.FreeLine;
                                break;
                            case "Text": currentShapeType = ShapeType.Text;
                                break;
                        }
                        shape = Shape.CreateShape(currentShapeType);
                        foreach (XElement xee in xe.Elements())
                        {
                            shape.readXmlElement(xee);
                        }

                        shape.Draw(graphics);

                        shapeList.Add(shape);
                        //fs1.Close();
                    }// End  xml
                }
            }
        }
コード例 #2
0
        private void openMenuItem_Click(object sender, EventArgs e)
        {
            //Open Menu & check condition
            if (dataModified)
            {
                // Offer to save drawing & display dialog
                DialogResult dialogResult = MessageBox.Show("Do you want to save changes?", "Form1", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question);
                switch (dialogResult)
                {
                case DialogResult.Yes: writeFile(currentFile);
                    break;

                case DialogResult.No: reset();
                    this.CreateGraphics().Clear(Form1.ActiveForm.BackColor);
                    break;

                case DialogResult.Cancel: return;
                }
            }

            //open dialog
            String         line = null;
            OpenFileDialog dlg  = new OpenFileDialog();

            dlg.Filter           = "Text files(*.txt)|*.txt|Binary Files(*.bin)|*.bin|Serialized files(*.ser)|*.ser|XML files(*.xml)|*.xml|All files(*.*)|*.*";
            dlg.InitialDirectory = Directory.GetCurrentDirectory();

            if (dlg.ShowDialog() == DialogResult.Cancel)
            {
                return;
            }

            reset();
            this.CreateGraphics().Clear(Form1.ActiveForm.BackColor);

            if (dlg.ShowDialog() == DialogResult.OK)
            {
                currentFile = dlg.FileName;
                //FileStream fs = new FileStream(currentFile, FileMode.Open);
                FileInfo fileInfo = new FileInfo(dlg.FileName);
                Graphics graphics = this.CreateGraphics();

                // check for text files & selecting the shape
                if (fileInfo.Extension == ".txt")
                {
                    FileStream   fs = new FileStream(currentFile, FileMode.Open);
                    StreamReader sr = new StreamReader(fs);
                    while ((line = sr.ReadLine()) != null)
                    {
                        switch (line)
                        {
                        case "Line": currentShapeType = ShapeType.Line;
                            break;

                        case "Rect": currentShapeType = ShapeType.Rectangle;
                            break;

                        case "FreeLine": currentShapeType = ShapeType.FreeLine;
                            break;

                        case "Text": currentShapeType = ShapeType.Text;
                            break;
                        }
                        shape = Shape.CreateShape(currentShapeType);

                        shape.readText(sr);
                        shape.Draw(graphics);
                        shapeList.Add(shape);
                    }
                    sr.Close();
                    fs.Close();
                }
                // Check for bin files & selecting encoding
                else if (fileInfo.Extension == ".bin")
                {
                    FileStream   fs = new FileStream(currentFile, FileMode.Open);
                    BinaryReader br = new BinaryReader(fs);

                    switch (br.ReadString())
                    {
                    case "us-ascii":
                        Shape.TextEncodingType = Encoding.ASCII;
                        break;

                    case "utf-8":
                        Shape.TextEncodingType = Encoding.UTF8;
                        break;

                    case "u\0t\0f\0-\01\06\0":
                        Shape.TextEncodingType = Encoding.Unicode;
                        break;

                    case "u\0\0\0t\0\0\0f\0\0\0-\0\0\03\0\0\02\0\0\0":
                        Shape.TextEncodingType = Encoding.UTF32;
                        break;

                    case "\0u\0n\0i\0c\0o\0d\0e\0F\0F\0F\0E":
                        Shape.TextEncodingType = Encoding.BigEndianUnicode;
                        break;

                    case "utf-7":
                        Shape.TextEncodingType = Encoding.UTF7;
                        break;
                    }

                    br = new BinaryReader(fs, Shape.TextEncodingType);

                    int length = (int)br.BaseStream.Length;
                    MessageBox.Show(length.ToString());
                    while (br.PeekChar() != -1)
                    {
                        // Selecting shape for bin file & recreating the figures
                        switch (br.ReadString())
                        {
                        case "Line":
                            currentShapeType = ShapeType.Line;
                            shape            = Shape.CreateShape(currentShapeType);
                            shape.readBinary(br);
                            shape.Draw(graphics);
                            shapeList.Add(shape);
                            break;

                        case "Rect":
                            currentShapeType = ShapeType.Rectangle;
                            shape            = Shape.CreateShape(currentShapeType);
                            shape.readBinary(br);
                            shape.Draw(graphics);
                            shapeList.Add(shape);
                            break;

                        case "FreeLine":
                            currentShapeType = ShapeType.FreeLine;
                            shape            = Shape.CreateShape(currentShapeType);
                            shape.readBinary(br);
                            shape.Draw(graphics);
                            shapeList.Add(shape);
                            break;

                        case "Text":
                            currentShapeType = ShapeType.Text;
                            shape            = Shape.CreateShape(currentShapeType);
                            shape.readBinary(br);
                            shape.Draw(graphics);
                            shapeList.Add(shape);
                            break;
                        }
                    }
                    fs.Close();
                }
                else if (fileInfo.Extension == ".ser")
                {
                    FileStream      fs = new FileStream(currentFile, FileMode.Open);
                    BinaryFormatter bf = new BinaryFormatter();
                    shapeList = (List <Shape>)bf.Deserialize(fs);

                    foreach (Shape shape in shapeList)
                    {
                        shape.Draw(graphics);
                    }
                    fs.Close();
                }//end ser
                else if (fileInfo.Extension == ".xml")
                {
                    XDocument document = XDocument.Load(currentFile);
                    XElement  root     = document.Root;
                    foreach (XElement xe in root.Elements())
                    {
                        switch (xe.Name.ToString())
                        {
                        case "Line": currentShapeType = ShapeType.Line;
                            break;

                        case "Rect": currentShapeType = ShapeType.Rectangle;
                            break;

                        case "FreeLine": currentShapeType = ShapeType.FreeLine;
                            break;

                        case "Text": currentShapeType = ShapeType.Text;
                            break;
                        }
                        shape = Shape.CreateShape(currentShapeType);
                        foreach (XElement xee in xe.Elements())
                        {
                            shape.readXmlElement(xee);
                        }

                        shape.Draw(graphics);

                        shapeList.Add(shape);
                        //fs1.Close();
                    }// End  xml
                }
            }
        }